diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-10-04 20:25:22 +0300 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-05 14:21:53 +0200 |
| commit | 77968899b90cd9afe2ed2668b290b66cb553ced2 (patch) | |
| tree | 4446ab6257f21603b45e04fadd4deeae231bffc9 /stdlib | |
| parent | c1713b4ea6c542fa2fe0d66b51c5b7b27fedf57a (diff) | |
Implement naive export system
Diffstat (limited to 'stdlib')
| -rw-r--r-- | stdlib/common.lisp | 86 | ||||
| -rw-r--r-- | stdlib/maybe.lisp | 60 |
2 files changed, 146 insertions, 0 deletions
diff --git a/stdlib/common.lisp b/stdlib/common.lisp new file mode 100644 index 0000000..2db5d62 --- /dev/null +++ b/stdlib/common.lisp @@ -0,0 +1,86 @@ +(let! compose (\[f g] + (\[x] (f (g x))))) + +(let! id (\[a] a)) + +(let! mod (\[n k] + (- n (* k (/ n k))))) + +(let! not (\[b] + (match b + true false + false true))) + +(let! is-even (\[n] + (match (mod n 2) + 0 true + 1 false))) + +(let! is-odd (compose not is-even)) + +;; map :: (a -> b) -> [a] -> [b] +(let! map (\[f lst] + (match lst + [] + [] + otherwise + (prepend (f (head lst)) (map f (tail lst)))))) + +(map (+ 1) [1 2 3]) + +;; foldr :: (a -> b -> b) -> b -> [a] -> b +(let! foldr (\[f accumulator lst] + (match lst + [] + accumulator + otherwise + (f (head lst) (foldr f accumulator (tail lst)))))) + +;; filter :: (a -> Bool) -> [a] -> [a] +(let! filter (\[pred lst] + (match lst + [] [] + otherwise (match (pred (head lst)) + true + (prepend (head lst) (filter pred (tail lst))) + false + (filter pred (tail lst)))))) + +(let! fibo (\[n] + (let! lazy fibo-1 (fibo (- n 1))) + (let! lazy fibo-2 (fibo (- n 2))) + (match n + 0 0 + 1 1 + _ (+ fibo-1 fibo-2)))) + +(let! reverse_ (\[v a] + (let! lazy x (head v)) + (let! lazy xs (tail v)) + (let! lazy xa (prepend x a)) + (match v + [] a + _ (reverse_ xs xa)))) + +(let! reverse (\[v] + (reverse_ v []))) + +(let! flow (\[fs] (foldr compose id (reverse fs)))) +(let! pipe (\[x fs] ((flow fs) x))) + +(let! lazy exports [ + compose + id + mod + not + is-even + is-odd + map + foldr + filter + fibo + reverse_ + reverse + flow + pipe +]) diff --git a/stdlib/maybe.lisp b/stdlib/maybe.lisp new file mode 100644 index 0000000..0bc7ac4 --- /dev/null +++ b/stdlib/maybe.lisp @@ -0,0 +1,60 @@ +; LIB + +(let! just (\[a] + ["maybe" "just" a])) +(let! nothing (\[] + ["maybe" "nothing"])) + +(let! unsafe-at (\[n seq] + (match seq + [] + (fatal! "unsafe-at out of bounds") + otherwise + (match n + 0 + (head seq) + otherwise + (unsafe-at (- n 1) (tail seq)))))) + +(let! unpack-just (unsafe-at 2)) +(let! kind (unsafe-at 1)) + +(let! map (\[f m] + (match (kind m) + "just" (just (f (unpack-just m))) + "nothing" (nothing)))) + +(let! and-then (\[f m] + (match (kind m) + "just" (f (unpack-just m)) + "nothing" (nothing)))) + +; TESTING + +;; (let! print-line! (\[s] +;; (print! (concat s "\n")))) + +;; (let! a (just 10)) +;; (let! b (nothing)) + +;; (map (+ 5) a) +;; (map (+ 5) b) + +;; (and-then (\[n] (just (+ n 5))) a) +;; (and-then (\[n] (just (+ n 5))) b) + +;; (let! m (just 10)) +;; (match m +;; (just _) +;; (print-line! (fmt "found just {0}!" [(unpack-just m)])) +;; (nothing) +;; (print-line! "found nothing!")) + +(let! lazy exports [ + just + nothing + unpack-just + kind + map + and-then +]) |
