More stdlib fun
parent
9edee10508
commit
aa7a034368
@ -1,9 +1,64 @@
|
|||||||
|
# map : (a \ b) \ List a \ List b
|
||||||
map = f list \ list
|
map = f list \ list
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [x, ...xs] \ [f x, ...map f xs];
|
| [x, ...xs] \ [f x, ...map f xs];
|
||||||
|
|
||||||
|
# filter : (a \ Bool) \ List a \ List a
|
||||||
filter = f list \ list
|
filter = f list \ list
|
||||||
| [] \ []
|
| [] \ []
|
||||||
| [x, ...xs] \ (f x
|
| [x, ...xs] \ (f x
|
||||||
| True \ [x, ...filter f xs]
|
| True \ [x, ...filter f xs]
|
||||||
| False \ filter f xs);
|
| False \ filter f xs);
|
||||||
|
|
||||||
|
# fold : (b \ a \ b) \ b \ List a \ b
|
||||||
|
fold = f acc list \ list
|
||||||
|
| [] \ acc
|
||||||
|
| [x, ...xs] \ fold f (f acc x) xs;
|
||||||
|
|
||||||
|
# reverse : List a \ List a
|
||||||
|
reverse = list \ list
|
||||||
|
| [] \ []
|
||||||
|
| [x, ...xs] \ (reverse xs) & [x];
|
||||||
|
|
||||||
|
# take : Int \ List a \ List a
|
||||||
|
take = n list \ { n = n, list = list }
|
||||||
|
| { n = _, list = [] } \ []
|
||||||
|
| { n = 0, list = _ } \ []
|
||||||
|
| { n = _, list = [x, ...xs] } \ [x, ...take (n - 1) xs];
|
||||||
|
|
||||||
|
# drop : Int \ List a \ List a
|
||||||
|
drop = n list \ { n = n, list = list }
|
||||||
|
| { n = _, list = [] } \ []
|
||||||
|
| { n = 0, list = _ } \ list
|
||||||
|
| { n = _, list = [x, ...xs] } \ drop (n - 1) xs;
|
||||||
|
|
||||||
|
# zipWith = (a \ b \ c) List a \ List b \ List c
|
||||||
|
zipWith = f l1 l2 \ l1
|
||||||
|
| [] \ []
|
||||||
|
| [x, ...xs] \ (l2
|
||||||
|
| [] \ []
|
||||||
|
| [y, ...ys] \ [f x y, ...zipWith f xs ys]);
|
||||||
|
|
||||||
|
# and : Bool \ Bool \ Bool
|
||||||
|
and = x y \ x
|
||||||
|
| False \ False
|
||||||
|
| True \ (y | False \ False | True \ True);
|
||||||
|
|
||||||
|
# or : Bool \ Bool \ Bool
|
||||||
|
or = x y \ x
|
||||||
|
| True \ True
|
||||||
|
| False \ (y | True \ True | False \ False);
|
||||||
|
|
||||||
|
# not = Bool \ Bool
|
||||||
|
not = x \ x
|
||||||
|
| True \ False
|
||||||
|
| False \ True;
|
||||||
|
|
||||||
|
# isSome = maybe \ ...
|
||||||
|
# isNone = maybe \ ...
|
||||||
|
# unwrapOr = default maybe \ ...
|
||||||
|
|
||||||
|
# range = start end \ ... # [start, start+1, ..., end-1]
|
||||||
|
# sum = list \ ...
|
||||||
|
# any = f list \ ...
|
||||||
|
# all = f list \ ...
|
||||||
|
|||||||
@ -1,7 +1,44 @@
|
|||||||
|
list = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
add1 = x \ x + 1;
|
add1 = x \ x + 1;
|
||||||
|
|
||||||
mapped = map add1 [1, 2, 3, 4, 5];
|
mapped = map add1 list;
|
||||||
|
|
||||||
isEven = x \ x % 2 == 0;
|
isEven = x \ x % 2 == 0;
|
||||||
|
|
||||||
filter isEven mapped
|
sumList = list \ fold add 0 list;
|
||||||
|
|
||||||
|
filtered = filter isEven mapped;
|
||||||
|
|
||||||
|
summed = sumList mapped;
|
||||||
|
|
||||||
|
l = len list;
|
||||||
|
|
||||||
|
reversed = reverse list;
|
||||||
|
|
||||||
|
take3 = take 3 list;
|
||||||
|
|
||||||
|
drop3 = drop 3 list;
|
||||||
|
|
||||||
|
list2 = [6, 7, 8];
|
||||||
|
|
||||||
|
zipped = zipWith add list list2;
|
||||||
|
|
||||||
|
anded = and True False;
|
||||||
|
|
||||||
|
ored = or True False;
|
||||||
|
|
||||||
|
notted = not False;
|
||||||
|
|
||||||
|
{
|
||||||
|
filtered = filtered,
|
||||||
|
summed = summed,
|
||||||
|
l = l,
|
||||||
|
reversed = reversed,
|
||||||
|
take3 = take3,
|
||||||
|
drop3 = drop3,
|
||||||
|
zipped = zipped,
|
||||||
|
anded = anded,
|
||||||
|
ored = ored,
|
||||||
|
notted = notted
|
||||||
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue