aboutsummaryrefslogtreecommitdiff
path: root/examples/fib.sloth
blob: 789ec5eacf192c214df1ef70fc6556ed53aa60b8 (plain)
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
fn fib(n: i32): i32 {
    val msg = if (n == 0)   "No points"   else    "Some points"
    val msg = if  n == 0  { "No points" } else  { "Some points" } 
    val msg = if  n == 0:   "No points"   else:   "Some points"

    if n == 0 || n == 1 {
        return n;
    }

    var grandparent = 0;
    var parent = 1;
    var me = 0;

    for i in 0..n-1 {
        me          = parent + grandparent;
        grandparent = parent;
        parent      = me;
    }

    return me;
}

pub type Button = {
    pub x: i32,
    pub y: i32,
    text: String,
}

impl Button {
    fn init(text) {
        return Self(
            x = 50,
            y = 150,
            text, # Pass in text
        )
    }
}

impl Constructor for Button {
    fn init(text) {
        Self(
            x = 50,
            y = 150,
            text, # Pass in text
        )
    }
}

Button::init("Hello")
Button("Hello")

print(fib(0));
print(fib(1));
print(fib(2));
print(fib(3));
print(fib(4));
print(fib(5));

# Inferred as List
val nums = read("input.txt")
    .lines()
    .filter(-> /$[0-9]+^/ in it)
    .collect()

fn T <- From<Self> = List collect(self): T {

}

# Statically typed with no type inference
fn fib(n: i32) -> i32:
    # ML
    match n with
    | 0 -> 0
    | 1 -> 1
    | n -> fib(n - 1) + fib(n - 2)

    # Python
    match n:
        case 0 | 1: n 
        case n:
            val lhs = fib(n - 1)
            val rhs = fib(n - 2)
            
            lhs + rhs

    # Idea
    match n:
        0 | 1: n
        n:
            val lhs = fib(n - 1)
            val rhs = fib(n - 2)

            lhs + rhs

# Statically typed but with type inference
fn fib(n):
    n + 1