-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path01_basics.affine
More file actions
53 lines (41 loc) · 2.09 KB
/
Copy path01_basics.affine
File metadata and controls
53 lines (41 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// SPDX-License-Identifier: MPL-2.0
// Warmup 01: Basic AffineScript — let bindings, literals, functions
//
// Try running each section with:
// affinescript eval 01_basics.affine
//
// The goal is to get your fingers used to AffineScript syntax before
// ownership and effects are introduced.
// ── Literals ──────────────────────────────────────────────────────────────────
let answer: Int = 42
let pi: Float = 3.14159
let greeting: String = "Hello, AffineScript"
let am_i_learning: Bool = true
let nothing: () = () // Unit — the absence of a value
// ── Let bindings are immutable by default ─────────────────────────────────────
let x = 10
let y = x + 5 // 15 — pure computation, no side effects yet
// ── Functions ─────────────────────────────────────────────────────────────────
// A simple function:
fn add(a: Int, b: Int) -> Int {
a + b
}
// Functions are values — you can store them in let bindings:
let double = fn(n: Int) -> Int { n * 2 }
// Generic functions work the same way:
fn identity[T](x: T) -> T {
x
}
// ── Pattern matching ──────────────────────────────────────────────────────────
fn describe_number(n: Int) -> String {
match n {
0 => "zero"
1 => "one"
_ => "many"
}
}
// ── Exercise 1: write a function that returns the absolute value of an Int ────
// fn abs(n: Int) -> Int { ... }
// ── Exercise 2: write a generic function `const_fn` that ignores its second
// argument and always returns the first ─────────────────────────────────────
// fn const_fn[A, B](a: A, _b: B) -> A { ... }