aboutsummaryrefslogtreecommitdiffstats
path: root/examples/test.lisp
blob: 27275a0ab137a17524d147629b93231f27420750 (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
(let compose (\[f g]
    (\[x] (f (g x)))))

((compose (+ 1) (+ 2)) 3)

(let mod (\[n k]
    (- n (* k (/ n k)))))

(let not (\[b]
    (match b
        true false
        false true)))

(let is-even (\[n]
    (match (mod n 2)
        0 true
        1 false)))

(let is-odd (compose not is-even))

;; map :: (a -> b) -> [a] -> [b]
(let map (\[f lst]
    (match lst
        [] []
        (prepend (f (head lst)) (map f (tail lst))))))

(map (+ 1) [1 2 3])

;; foldr :: (a -> b -> b) -> b -> [a] -> b
(let foldr (\[f accumulator lst]
    (match lst
        []  accumulator
        (f (head lst) (foldr f accumulator (tail lst))))))

(foldr + 0 [1 2 3])

;; filter :: (a -> Bool) -> [a] -> [a]
(let filter (\[pred lst]
    (match lst
        [] []
        (match (pred (head lst))
            true (prepend (head lst) (filter pred (tail lst)))
            false (filter pred (tail lst))))))

(filter is-even [0 1 2 3 4 5])

(let fibo (\[n]
    (let lazy fibo-1 (fibo (- n 1)))
    (let lazy fibo-2 (fibo (- n 2)))
    (match n
        0  0
        1  1
        (+ fibo-1 fibo-2))))

(fibo 10)

(let reverse_ (\[v a]
    (let lazy x (head v))
    (let lazy xs (tail v))
    (let lazy xa (prepend x a))
    (match v
        [] a
        (reverse_ xs xa))))

(let reverse (\[v]
    (reverse_ v [])))

(reverse [1 2 3])