forked from nulang-org/nulang
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path03_functions.nula
More file actions
40 lines (34 loc) · 1.07 KB
/
Copy path03_functions.nula
File metadata and controls
40 lines (34 loc) · 1.07 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
// 03_functions.nula - Functions, closures, and recursion
// Demonstrates: fn, let rec, multi-arg closures, blocks
//
// Run: nulang examples/03_functions.nula
// Simple closure
let greet = fn(name) {
"Hello, " + name + "!"
}
perform IO.print(greet("Alice"))
// Multi-argument closure
let add = fn(x, y) { x + y }
let multiply = fn(x, y) { x * y }
let sum = add(40, 2)
let product = multiply(6, 7)
perform IO.print("40 + 2 = " + perform Int.to_string(sum))
perform IO.print("6 * 7 = " + perform Int.to_string(product))
// Block expression returning a value
let result = {
let a = 10
let b = 20
a + b
}
perform IO.print("Block result: " + perform Int.to_string(result))
// Recursive closure with let rec
let rec factorial = fn(n) {
if n <= 1 then 1 else n * factorial(n - 1)
}
perform IO.print("5! = " + perform Int.to_string(factorial(5)))
perform IO.print("10! = " + perform Int.to_string(factorial(10)))
// Recursive Fibonacci
let rec fib = fn(n) {
if n <= 1 then n else fib(n - 1) + fib(n - 2)
}
perform IO.print("fib(10) = " + perform Int.to_string(fib(10)))