-
Notifications
You must be signed in to change notification settings - Fork 0
/
prelude.u
151 lines (140 loc) · 3.27 KB
/
prelude.u
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
; for historical pleasure
(def S (\x y a ((x a) (y a))))
(def K (\x y x))
; functional tools
(def id (\x x))
(def recur (\f ((\x (f (x x))) (\x (f (x x))))))
(def print id)
(def die (recur id)) ; this makes the program run forever
(def const K)
(def . (\f g x (f (g x))))
(def apply id)
(def feed (\x y (y x)))
(def flip (\f y x (f x y)))
(def iterateN (recur (\g i f x
((<= i 0) x
(f (g (- i 1) f x))
)
)))
; the Bool protocol: (b true_value false_value)
(def True (\x y x))
(def False (\x y y))
(def if id)
(def not (\x (x False True)))
(def and (\x y (x y False)))
(def or (\x y (x True y)))
; the Pair protocol: (p (\first second pair_value))
(def pair (\x y f (f x y)))
(def fst (feed True))
(def snd (feed False))
; the List protocol: (l null_value (\head tail list_value))
(def cons (\x xs f g (g x xs)))
(def empty True)
(def null (\x (x True (\_ _ False))))
(def foldr (\f x0 (recur (\g (\l
(l x0 (\x xs
(f x (g xs))
)))))
))
(def foldl (\f (recur (\g (\x0 l
(l x0 (\x xs
(g (f x0 x) xs))
))))
))
(def foldr1 (\f f1 x0 (recur (\g l
(l x0 (\x xs
(xs (f1 x) (\_ _
(f x (g xs))
))
))))
))
(def ++ (\a b (foldr (\x y (cons x y)) b a)))
(def join (\sep (foldr1 (\x y (++ x (++ sep y))) id empty)))
(def concat (foldr ++ empty))
(def intersperse (\sep (foldr1 (\x y (cons x (cons sep y))) (\x (list x)) empty)))
(def sum (foldl + 0))
(def map (\f (foldr (\h r (cons (f h) r)) empty)))
(def cmpList (\cmp (recur (\f (\l1 l2
(l1 (l2 False (\_ _ True)) (\h1 r1 (l2 False (\h2 r2
((cmp h1 h2) True
((cmp h2 h1) False
(f r1 r2)
)
)
))))
)))))
(def eqList (\eq (recur (\f (\l1 l2
(l1 (l2 True (\_ _ False)) (\h1 r1 (l2 False (\h2 r2
((eq h1 h2) (f r1 r2) False)
))))
)))))
(def dropWhile (\cond (recur (\f (\l
(l l (\h r
((cond h) (f r) l)
))
)))))
(def takeWhile (\cond (recur (\f (\l
(l l (\h r
((cond h) (cons h (f r)) empty)
))
)))))
(def span (\cond (recur (\f (\l
(l (pair l l) (\h r
((cond h) ((f r) (\l1 l2 (pair (cons h l1) l2)))
(pair empty l)
)
))
)))))
(def zipWith (\f (recur (\g a b
(a empty (\ha ra
(b empty (\hb rb
(cons (f ha hb) (g ra rb))
))
))
))))
(def zip (zipWith pair))
(def scanl (\f (recur (\g x0 l
(l (list x0) (\h r
(cons x0 (g (f x0 h) r))
))
))))
(def iterate (\f (recur (\g x (cons x (g (f x)))))))
(def length (recur (\self l (l 0 (\_ r (+ (self r) 1))))))
(def any (\p (recur (\f (\l (l False (\h r ((p h) True (f r)))))))))
(def all (\p (recur (\f (\l (l True (\h r ((p h) (f r) False))))))))
; the Maybe protocol: (m nothing_value (\v just_value))
(def nothing True)
(def just (\v (\f g (g v))))
; numeric utils
(def neg (- 0))
; IO macros
(let fileName (recur (\f (\name
(name
makeIntList
(\h r ((f r) h))
)
))))
(def open (\f (openCmd (fileName f))))
(def system (\f (systemCmd (fileName f))))
(def ReadMode 0)
(def WriteMode 1)
(def AppendMode 2)
(def ReadWriteMode 3)
(def stdin 0)
(def stdout 1)
; run macros
(def ^list (\l f (foldl apply f l)))
(def ^2 (\e c l (l e c)))
(def ^3 (\a b c f (f a b c)))
(def ^4 (\a b c d f (f a b c d)))
(def ^5 (\a b c d e f (f a b c d e)))
(def ^6 (\a b c d e f g (g a b c d e f)))
(def @0_1 (\x f (f x)))
(def @0_2 (\x f g (f x)))
(def @1_2 (\x f g (g x)))
(def !0_1 (\a a))
(def !0_2 (\a b a))
(def !1_2 (\a b b))
(def !0_3 (\a b c a))
(def !1_3 (\a b c b))
(def !2_3 (\a b c c))