fibo.kl
1// You can link any symbol from libc here
2
3extern fn putchar(c: uint);
4
5fn main() {
6 n := 0;
7 while n != 10 {
8 res := fibo(n);
9 print_num(res);
10 // newline
11 putchar(10);
12
13 n = n + 1;
14 }
15}
16
17fn num_to_ascii(n: uint) uint /*Option<uint>*/ {
18 if n <= 9 {
19 num_ascii_base := 48;
20 // Some(num_ascii_base + n)
21 num_ascii_base + n
22 } else {
23 // None
24 21
25 }
26}
27
28fn print_num_rev(n: uint) {
29 if n == 0 {
30 putchar(num_to_ascii(0));
31 } else {
32 while n != 0 {
33 rest := n % 10;
34 n = n / 10;
35 putchar(num_to_ascii(rest));
36 }
37 };
38}
39
40fn print_num(n: uint) {
41 if n == 0 {
42 putchar(num_to_ascii(0));
43 return;
44 };
45
46 // Calculate the divisor to get the most significant digit
47 divisor := 1;
48 temp := n;
49 while temp >= 10 {
50 temp = temp / 10;
51 divisor = divisor * 10;
52 };
53
54 // Reset temp to original number
55 temp = n;
56
57 // Print each digit from most to least significant
58 while divisor > 0 {
59 digit := temp / divisor; // Get the current most significant digit
60 putchar(num_to_ascii(digit));
61
62 // Remove the most significant digit
63 temp = temp % divisor;
64
65 // Reduce divisor
66 divisor = divisor / 10;
67 };
68}
69
70
71fn fibo(n: uint) uint {
72 a := 0;
73 b := 1;
74
75 while n != 0 {
76 next := a + b;
77 a = b;
78 b = next;
79
80 n = n - 1;
81 }
82
83 b
84}
85