1
- # scott-lists .lc
1
+ # scott-list .lc
2
2
3
3
#import combinators.lc
4
4
B = \ f g x . f (g x)
@@ -18,7 +18,7 @@ Y = \ f . ( \ x . f (x x) ) ( \ x . f (x x) )
18
18
#import scott-booleans.ls
19
19
False = K
20
20
True = KI
21
- not = \ p . p True False
21
+ not = C
22
22
and = M
23
23
or = W C
24
24
#import scott-ordering.lc
@@ -58,7 +58,7 @@ is-none = \ x . x True (K False) # = is-zero
58
58
is-some = \ x . x False (K True)
59
59
from-option = \ z x . x z I
60
60
from-some = \ x . x () I
61
- # additional definitions depend on nil and cons
61
+ # additional definitions depend on nil, cons, singleton
62
62
63
63
# data List a = Nil | Cons a (List a)
64
64
@@ -71,32 +71,43 @@ cons = \ x xs . \ _nil cons . cons x xs
71
71
# singleton :: a -> List a
72
72
singleton = \ x . cons x nil
73
73
74
- # these scott-options definitions depend on nil, cons, singleton
74
+ # these scott-option definitions depend on nil, cons, singleton
75
75
list-to-option = \ xs . xs None \ x _xs . Some x
76
76
option-to-list = \ x . x nil singleton
77
77
map-option = \ fn xs . xs nil \ x xs . fn x (map-option fn xs) (C cons (map-option fn xs))
78
78
cat-options = map-option I
79
79
80
- # continuing scott-lists .lc
80
+ # continuing scott-list .lc
81
81
82
82
# foldr :: (a -> z -> z) -> z -> List a -> z
83
- foldr = \ fn z xs . xs z ( \ x xs . fn x (foldr fn z xs) )
83
+ foldr = \ fn z xs . xs z \ x xs . fn x (foldr fn z xs)
84
84
85
- # null :: List a -> Boolean
86
- null = \ xs . xs True (KK False)
85
+ # foldl :: (z -> a -> z) -> z -> List a -> z
86
+ foldl = \ fn z xs . xs z (B (foldl fn) (fn z))
87
+
88
+ # scanr :: (a -> z -> z) -> z -> List a -> List z
89
+ scanr = \ fn z xs . xs (singleton z) \ x xs . ( \ zs . zs () \ z _zs . cons (fn x z) zs ) (scanr fn z xs)
90
+
91
+ # scanl :: (z -> a -> z) -> z -> List a -> List z
92
+ scanl = \ fn z xs . cons z (xs nil (B (scanl fn) (fn z)))
87
93
88
94
# take :: Number -> List a -> List a
89
95
take = \ n xs . is-zero n (xs nil \ x xs . cons x (take (pred n) xs)) nil
90
96
97
+ # drop :: Number -> List a -> List a
98
+ drop = \ n xs . is-zero n (xs nil (K (drop (pred n)))) xs
99
+
91
100
# append :: List a -> List a -> List a
92
101
append = C (foldr cons)
93
102
94
103
# concat :: List (List a) -> List a
95
- concat = \ xss . foldr xss append nil
104
+ concat = foldr append nil
96
105
97
- # sum,product :: List Number -> Number
98
- sum = foldr add zero
99
- product = foldr mul one
106
+ # snoc :: List a -> a -> List a
107
+ snoc = C (B (foldr cons) singleton)
108
+
109
+ # uncons :: List a -> Option (Pair a (List a))
110
+ uncons = \ xs . xs None (BB Some Pair)
100
111
101
112
# iterate :: (a -> a) -> a -> List a
102
113
iterate = \ fn x . cons x (iterate fn (fn x))
@@ -105,47 +116,57 @@ iterate = \ fn x . cons x (iterate fn (fn x))
105
116
repeat = \ x . cons x (repeat x) # repeat = Y (S cons)
106
117
107
118
# cycle :: List a -> List a
108
- cycle = \ xs . null xs (concat (repeat xs)) ( )
119
+ cycle = \ xs . xs () ( concat (repeat xs))
109
120
110
121
# replicate :: Number -> a -> List a
111
122
replicate = \ n . B (take n) repeat
112
123
124
+ # unfold :: (a -> Option (Pair z a)) -> a -> List z
125
+ unfold = \ fn x . fn x nil (T \ z x . cons z (unfold fn x))
126
+
113
127
# head :: List a -> a
114
128
head = \ xs . xs () K
115
129
116
130
# tail :: List a -> List a
117
131
tail = \ xs . xs () KI
118
132
133
+ # null :: List a -> Boolean
134
+ null = \ xs . xs True (KK False)
135
+
119
136
# length :: List a -> Number
120
137
length = foldr (K succ) zero
121
138
122
- # snoc :: List a -> a -> List a
123
- snoc = C (B (foldr cons) singleton)
139
+ # sum,product :: List Number -> Number
140
+ sum = foldr add zero
141
+ product = foldr mul one
124
142
125
143
# map :: (a -> b) -> List a -> List b
126
144
map = \ fn . foldr (B cons fn) nil
127
145
128
146
# concat-map :: (a -> List b) -> List a -> List b
129
147
concat-map = BB concat map
130
148
131
- # filter :: () -> List a -> List a
149
+ # filter :: (a -> Boolean ) -> List a -> List a
132
150
filter = \ p . foldr ( \ x z . p x z (cons x z) ) nil
133
- filter = \ p . foldr ( \ x . S (p x) (cons x) ) nil
134
- filter = \ p . foldr (S (B S p) cons) nil
135
151
136
- # drop :: Number -> List a -> List a
137
- drop = \ n xs . is-zero n ( \ _x xs . drop (pred n) xs ) xs
138
- drop = \ n . is-zero n (K (drop (pred n)))
152
+ # take-while :: (a -> Boolean) -> List a -> List a
153
+ take-while = \ p xs . xs nil \ x xs . p x nil (cons x (take-while p xs))
154
+
155
+ # drop-while :: (a -> Boolean) -> List a -> List a
156
+ drop-while = \ p xs . xs nil \ x xs . p x xs (drop-while p xs)
157
+
158
+ # drop-while-end :: (a -> Boolean) -> List a -> List a
159
+ drop-while-end = \ p . foldr ( \ x z . and (null z) (p x) (cons x z) nil ) nil
139
160
140
161
# split-at :: Number -> List a -> Pair (List a) (List a)
141
162
split-at = \ i xs . is-zero i (xs (Pair nil nil) \ x xs . first (cons x) (split-at (pred i) xs)) (Pair nil xs)
142
163
143
164
# get :: Number -> List a -> a
144
- get = \ i xs . is-zero i ( \ x xs . xs () (get (pred i) xs) ) (head xs)
165
+ get = \ i xs . is-zero i (xs () (K ( get (pred i))) ) (head xs)
145
166
146
167
# set :: Number -> a -> List a -> List a
147
168
set = \ i x xs . uncurry append (second (B (cons x) tail) (split-at i xs))
148
- set = \ i x xs . is-zero i (xs nil \ y ys . cons y (set (pred i) x ys )) (xs nil (K (cons x)))
169
+ set = \ i x xs . is-zero i (xs nil \ y . cons y (set (pred i) x)) (xs nil (K (cons x)))
149
170
150
171
# any :: (a -> Boolean) -> List a -> Boolean
151
172
any = \ p . foldr (B or p) False
@@ -154,96 +175,78 @@ any = \ p . foldr (B or p) False
154
175
all = \ p . foldr (B and p) True
155
176
156
177
# find :: (a -> Boolean) -> List a -> Option a
157
- find = \ p . foldr ( \ x z . p x z (Some x) ) None
178
+ find = BB list-to-option filter
158
179
159
180
# find-indices :: (a -> Boolean) -> List a -> List Number
160
181
find-indices = \ p . foldr ( \ x k i . p x I (cons i) (k (succ i)) ) (K nil) zero
161
182
162
183
# find-index :: (a -> Boolean) -> List a -> Option Number
163
- find-index = \ p . B list-to-option ( find-indices p)
184
+ find-index = BB list-to-option find-indices
164
185
165
186
# partition :: (a -> Boolean) -> List a -> Pair (List a) (List a)
166
187
partition = \ p . foldr ( \ x . p x second first (cons x) ) (Pair nil nil)
167
188
168
189
# span :: (a -> Boolean) -> List a -> Pair (List a) (List a)
169
190
span = \ p xs . xs (Pair nil nil) \ y ys . p y (Pair nil xs) (first (cons y) (span p ys))
170
191
171
- # minimum-by :: (a -> a -> Boolean) -> List a -> a # cmp ~ le
172
- minimum-by = \ cmp xs . xs () (foldr \ x z . cmp x z z x)
173
-
174
- # maximum-by :: (a -> a -> Boolean) -> List a -> a # cmp ~ le
175
- maximum-by = \ cmp xs . xs () (foldr \ x z . cmp x z x z)
192
+ # minimum-by :: (a -> a -> Boolean) -> List a -> a
193
+ minimum-by = \ le xs . xs () (foldl \ z x . le z x x z)
176
194
177
- # insert -by :: (a-> a -> Boolean) -> a -> List a -> List a # cmp ~ le
178
- insert -by = \ cmp x xs . uncurry append (second (cons x) (span (C cmp x) xs) )
195
+ # maximum -by :: (a -> a -> Boolean) -> List a -> a
196
+ maximum -by = \ le xs . xs () (foldl \ z x . le z x z x )
179
197
180
- # sort-by :: (a -> a -> Boolean) -> List a -> List a # cmp ~ le
181
- sort-by = \ cmp . foldr (insert-by cmp) nil
182
-
183
- # foldl :: (z -> a -> z) -> z -> List a -> z
184
- foldl = \ fn z xs . xs z (B (foldl fn) (fn z))
185
-
186
- # scanl :: (z -> a -> z) -> z -> List a -> List z
187
- scanl = \ fn z xs . cons z (xs nil (B (scanl fn) (fn z)))
198
+ # insert-by :: (a -> a -> Boolean) -> a -> List a -> List a
199
+ insert-by = \ le x xs . uncurry append (second (cons x) (span (C le x) xs))
188
200
189
- # scanr :: (a -> z -> z) -> z -> List a -> List z
190
- scanr = \ fn z xs . xs (singleton z) \ x xs . ( \ zs . zs \ z _zs . cons (fn x z) zs ) (scanr fn z xs)
201
+ # sort-by :: (a -> a -> Boolean) -> List a -> List a
202
+ sort-by = \ le . foldr (insert-by le) nil
203
+ # has all sorts of bad implementation details, but it's simple
191
204
192
205
# reverse :: List a -> List a
193
206
reverse = foldl (C cons) nil
194
207
195
- # unzip :: List (Pair a b) -> Pair (List a) (List b)
196
- unzip = foldr ( \ xy xys . xy \ x y . bimap (cons x) (cons y) xys ) (Pair nil nil)
197
- unzip = foldr (CB \ x y . bimap (cons x) (cons y)) (Pair nil nil)
198
-
199
208
# zip-with :: (a -> b -> z) -> List a -> List b -> List z
200
209
zip-with = \ fn xs ys . xs nil \ x xs . ys nil \ y ys . cons (fn x y) (zip-with fn xs ys)
201
210
202
211
# zip :: List a -> List b -> List (Pair a b)
203
212
zip = zip-with Pair
204
213
205
- # init :: List a -> List a
206
- init = \ xs . xs () (S (zip-with K) tail xs)
207
-
208
- # last :: List a -> a
209
- last = foldl KI ()
210
-
211
- # slice :: Number -> Number -> List a -> List a
212
- slice = \ i j xs . gt j i nil (take (sub j i) (drop i xs))
213
-
214
- # uncons :: List a -> Option (Pair (a) (List a))
215
- uncons = \ xs . xs None (B Some Pair)
214
+ # unzip :: List (Pair a b) -> Pair (List a) (List b)
215
+ unzip = foldr ( \ xy xys . xy \ x y . bimap (cons x) (cons y) xys ) (Pair nil nil)
216
+ unzip = foldr (CB \ x y . bimap (cons x) (cons y)) (Pair nil nil)
216
217
217
- # transpose :: List (List a) -> List (List a)
218
- transpose = \ xss . xss nil
219
- \ ys yss . ys (transpose yss)
220
- (unzip (map-option uncons xss) \ xs xxs . cons xs (transpose xss))
218
+ # group-by :: (a -> a -> Bool) -> List a -> List (List a)
219
+ group-by = \ eq xs . xs nil \ x xs . span (eq x) xs \ left right . cons (cons x left) (group-by eq right)
221
220
222
- # unfold :: (a -> Option (Pair z a)) -> a -> List z
223
- unfold = \ fn x . fn x nil (T \ z x . cons z (unfold fn x) )
221
+ # lookup-by :: (a -> Boolean) -> List (Pair a b) -> Option b
222
+ lookup-by = \ p xys . xys None \ xy xys . xy \ x y . p x (lookup-by p xys) (Some y )
224
223
225
- # take-while :: (a -> Boolean) -> List a -> List a
226
- take-while = \ p xs . xs nil \ x xs . p x nil (cons x (take-while p xs))
224
+ # nub-by :: (a -> a -> Boolean) -> List a -> List a
225
+ go = \ z eq xs . xs z \ x xs . go (is-none (find (eq x) z) z (cons x z)) eq xs
226
+ nub-by = go nil
227
227
228
- # drop-while :: (a -> Boolean) -> List a -> List a
229
- drop-while = \ p xs . xs nil \ x xs . p x xs (drop-while p xs)
228
+ # delete-by :: (a -> a -> Boolean) -> a -> List a -> List a
229
+ delete-by = \ eq x xs . xs nil \ y ys . eq x y (cons y (delete-by eq x ys)) ys
230
230
231
- # drop-while-end :: (a -> Boolean) -> List a -> List a
232
- drop-while-end = \ p . foldr ( \ x z . and (null z) (p x) (cons x z) nil ) nil
231
+ # delete-firsts-by :: (a -> a -> Boolean) -> List a -> List a -> List a
232
+ delete-firsts-by = \ eq . foldl (C (delete-by eq))
233
233
234
- # group-by :: (a -> a -> Bool) -> List a -> List (List a)
235
- group-by = \ eq xs . xs nil \ x xs . span (eq x) xs \ left right . cons (cons x left) (group-by eq right)
236
- group-by = \ eq xs . xs nil \ x xs . uncurry cons (bimap (cons x) (group-by eq) (span (eq x) xs))
234
+ # init :: List a -> List a
235
+ init = \ xs . xs () (S (zip-with K) tail xs)
237
236
238
- # inits
237
+ # last :: List a -> a
238
+ last = foldl KI ()
239
239
240
240
# tails :: List a -> List (List a)
241
241
tails = \ xs . cons xs (xs nil (K tails))
242
242
243
- # lookup-by :: ( a -> Boolean) -> List (Pair a b) -> Option b
244
- lookup-by = \ eq xys . xys None \ xy xys . xy \ x y . eq x (lookup-by eq xys ) (Some y )
243
+ # inits :: List a -> List (List a)
244
+ inits = \ xs . xs (singleton nil) \ x xs . cons nil (map (cons x ) (inits xs) )
245
245
246
- # nub-by
247
- # delete-by
248
- # delete-firsts-by
249
- # sort-on
246
+ # slice :: Number -> Number -> List a -> List a
247
+ slice = \ i j xs . le i j nil (take (sub j i) (drop i xs))
248
+
249
+ # transpose :: List (List a) -> List (List a)
250
+ transpose = \ xss . xss nil
251
+ \ ys yss . ys (transpose yss)
252
+ (unzip (map-option uncons xss) \ xs xss . cons xs (transpose xss))
0 commit comments