]> git.eli173.com Git - klapaucius/blob - demos.kl
reworking towards full proper reference counting implementation
[klapaucius] / demos.kl
1 (def comment (rest (K nil)))
2
3 (def CC (B (C B (C B cdr)) (B (Phi cons) (C B car))))
4
5 (def D (B B)) (comment Dabcd -> ab(cd))
6 (def T (C I)) (comment Tab -> ba)
7 (def V (B C T)) (comment Vabc -> cab)
8 (def O (S I)) (comment Oab -> b(ab))
9 (def M (S I I)) (comment Ma -> aa)
10 (def Q (C B)) (comment Qabc -> b(ac))
11 (def L (C B M)) (comment Lab -> a(bb))
12 (def G (B B C)) (comment Gabcd -> ad(bc))
13 (def E (B (B B B))) (comment Eabcde -> ab(cde))
14 (def R (B B T)) (comment Rabc -> bca)
15 (def H (B W (B C))) (comment Habc -> abcb)
16 (def U (L O)) (comment Uab -> b(aab))
17 (def F (E T T E T)) (comment Fabc -> cba)
18
19 (def B1 (B B B)) (comment B1abcd -> a(bcd))
20 (def B2 (B B1 B)) (comment B2abcde -> a(bcde))
21 (def B3 (B (B B) B)) (comment B3abcd -> a(b(cd)))
22
23 (def C* (B C))
24 (def C** (B C*))
25 (def C*** (B C**))
26
27 (def C*n (Z (B (S (C (eq 0) C)) (B D (C B (C - 1))))))
28
29 (def C1 (C* (B C)))
30 (def C2 (C* (B C1)))
31 (def C3 (C* (B C2)))
32
33 (def Cn (Z (B (S (C (eq 0) C)) (B (B (B C)) (B D (C B (C - 1)))))))
34
35 (def import (B (B B) B (map unquote) parse readfile))
36 (comment apparently it is that easy)
37
38
39 (def abstract-generator (Z (B (B (B (B (C (C C nil))))) (B (Phi B (B S (B cons))) (B (B W) (B (B B)))))))
40
41 (def abstract-rec (B (B (B (C B cons))) (B (B (B B)) (B (B (B Z)) (C (B B (B B (B B (B S (C (C Phi cdr) car))))) (B (B (C B)) (B (C B (C B cdr)) (B (Phi cons) (C (C Phi car) cdr)))))))))
42
43
44 (def zd-identity I)
45 (def zd-constant K)
46 (def zd-compose B)
47 (def zd-flip C)
48 (def zd-duplicate W)
49 (def zd-left t)
50 (def zd-right nil)
51 (def zd-recombine Phi)
52 (def zd-under Psi)
53 (def zd-conditional (B (B S) S))
54
55 (def if I)
56
57 (def church-and (B W (B C) I))
58 (def church-or (S I I))
59 (def church-not (B C (C I) nil t))
60 (def church-xor (C (C Phi not) I))
61 (def church-zerop (B C (C I) (t nil) t))
62
63 (def fac (Z (D S (C (eq 0) 1) (D S * (C B (C - 1))))))
64
65 (def range (abstract-rec (eq 0) (C cons) (C - 1) nil))
66 (comment def range (S (C (eq 0) nil) (B (Z (B B S (S (B (eq 0) car) cdr) (C B (Phi cons (B (C - 1) car) (Phi cons car cdr))))) (C cons nil))))
67
68 (def reverse (abstract-rec not (C (B cons car)) cdr nil))
69 (comment def reverse (B (Z (B (S (S (B (eq nil) cdr) car)) (C B (Phi cons (Phi cons (B car cdr) car) (B cdr cdr))))) (cons nil)))
70
71 (def nth (B (B car) (B (C I cdr) (C - 1))))
72
73 (def list (rest (map unquote)))
74
75 (def len (abstract-rec not (B K (+ 1)) cdr 0))
76 (comment def len (B (Z (B (S (S (B (eq nil) cdr) car)) (C B (CC (+ 1) cdr)))) (cons 0)))
77
78 (def map (B (B reverse) (B (B (Z (B (S (S (B (eq nil) (2 cdr)) (B car cdr))) (C B (S (B cons car) (Phi cons (Phi cons (S car (B (B car cdr) cdr)) (B car cdr)) (3 cdr))))))) (C (B B cons) (cons nil)))))
79
80 (def fold (B (S (C (C (eq nil) nil) (Z (B (S (S (B (eq nil) (B cdr cdr)) (B car cdr))) (C B (S (B cons car) (Phi cons (S (S car (B car cdr)) (B car (2 cdr))) (3 cdr)))))))) cons))
81
82
83 (def compose-all (rest (B (fold (B (C B unquote) B)) (cons I))))
84
85 (def fibonacci-generator (Z (B (B (C (C C nil))) (B (Phi cons (Phi + car cdr)) (C B (Phi cons (Phi + car cdr) car))))))
86
87 (comment def tr-fac (B (S (Phi (eq 0) cdr car)) (C B (Phi cons (Phi * car cdr) (B (C - 1) cdr)))))
88
89 (def append (B (C (abstract-rec not (B (C B car) (C cons)) cdr)) reverse))
90
91 (def filter (C (C (B (abstract-rec not) (B W (B (B C) (C (B C (B (B B) (C (B B S) (C cons)))) car)))) cdr) nil))
92
93 (def qsort ((B Z (B (B (S (W atom))) (Phi (Phi (Phi append)) (B (C B) (C (B (Phi filter) (B (B (B not)) (C B car))) cdr)) (B (B (Phi cons car)) (B (C B) (C (B (Phi filter) (C B car)) cdr))))))))
94
95 (def x-times-n (C (C (D abstract-rec (eq 0) (D B K cons)) (C - 1)) nil))
96 (comment (x-times-n x 5) -> (x x x x x))
97
98 (def m-rand-n (B (C (C (D abstract-rec (eq 0) (D B K cons)) (C - 1)) nil) rand))