|
| 1 | +-- Useful functions for G-Machine programs |
| 2 | +-- The CorePrelude already defines the basic SKI combinators |
| 3 | +-- and `if`, `True`, `False`, `Cons`, and `Nil` |
| 4 | + |
| 5 | +-- Logical operators |
| 6 | +and x y = if x y False ; |
| 7 | +or x y = if x True y ; |
| 8 | +xor x y = if x (not y) y ; |
| 9 | +not x = if x False True ; |
| 10 | + |
| 11 | +-- Useful simple combinators |
| 12 | +id x = x ; |
| 13 | +double x = x + x ; |
| 14 | + |
| 15 | +-- Useful simple arithmetic operators and predicates |
| 16 | +inc x = x + 1 ; |
| 17 | +zero x = x == 0 ; |
| 18 | +even x = x == ((x / 2) * 2) ; |
| 19 | +odd = compose not even ; |
| 20 | +mod m n = m - (m / n) * n ; |
| 21 | + |
| 22 | +add acc val = acc + val ; |
| 23 | +mul acc val = acc * val ; |
| 24 | + |
| 25 | +-- Sample math functions |
| 26 | +fac n = if (zero n) 1 (n * fac (n - 1)) ; |
| 27 | +fib n = if (n < 2) n (fib (n - 1) + fib (n - 2)) ; |
| 28 | + |
| 29 | +-- List operations, LISP-style |
| 30 | +car l = case l of |
| 31 | + <4> x y -> x ; |
| 32 | +cdr l = case l of |
| 33 | + <4> x y -> y ; |
| 34 | +length l = case l of |
| 35 | + <3> -> 0 ; |
| 36 | + <4> x y -> 1 + length y ; |
| 37 | +nth n l = case l of |
| 38 | + <3> -> 0 ; |
| 39 | + <4> x xs -> if (zero n) x (nth (n - 1) xs) ; |
| 40 | + |
| 41 | +caar = compose car car ; |
| 42 | +cadr = compose car cdr ; |
| 43 | +cdar = compose cdr car ; |
| 44 | +cddr = compose cdr cdr ; |
| 45 | +caadr = compose car cadr ; |
| 46 | +caaar = compose car caar ; |
| 47 | +cdadr = compose cdr cadr ; |
| 48 | +cdaar = compose cdr caar ; |
| 49 | +caddr = compose car cddr ; |
| 50 | +cadar = compose car cdar ; |
| 51 | +cdddr = compose cdr cddr ; |
| 52 | +cddar = compose cdr cdar ; |
| 53 | + |
| 54 | +-- Sequences/streams |
| 55 | +numsFrom x = Cons x (numsFrom (x + 1)) ; |
| 56 | +nats = numsFrom 0 ; |
| 57 | +ones = Cons 1 ones ; |
| 58 | + |
| 59 | +-- List operations |
| 60 | +map f lst = case lst of |
| 61 | + <3> -> Nil ; |
| 62 | + <4> x xs -> Cons (f x) (map f xs) ; |
| 63 | +filter p lst = |
| 64 | + case lst of |
| 65 | + <3> -> Nil ; |
| 66 | + <4> x xs -> |
| 67 | + let rest = filter p xs in |
| 68 | + if (p x) (Cons x rest) rest ; |
| 69 | +foldl f acc lst = case lst of |
| 70 | + <3> -> acc ; |
| 71 | + <4> x xs -> foldl f (f acc x) xs ; |
| 72 | +take n lst = if (n <= 0) Nil (take2 n lst) ; |
| 73 | +take2 n lst = case lst of |
| 74 | + <3> -> Nil ; |
| 75 | + <4> x xs -> Cons x (take (n - 1) xs) ; |
| 76 | +takeWhile p lst = case lst of |
| 77 | + <3> -> Nil ; |
| 78 | + <4> x xs -> if (p x) (Cons x (takeWhile p xs)) Nil ; |
| 79 | +drop n lst = if (n <= 0) lst (drop2 n lst) ; |
| 80 | +drop2 n lst = case lst of |
| 81 | + <3> -> Nil ; |
| 82 | + <4> x xs -> drop (n - 1) xs ; |
| 83 | + |
| 84 | +-- Aggregate operations |
| 85 | +sum = foldl add 0 ; |
| 86 | +prod = foldl mul 1 ; |
| 87 | + |
| 88 | +-- Useful function to generate a range |
| 89 | +range a b = map (add a) (take ((b - a) + 1) nats) |
0 commit comments