aboutsummaryrefslogtreecommitdiffstats
path: root/examples/test.lisp
diff options
context:
space:
mode:
authorJan Tuomi <jan.tuomi@valuemotive.com>2022-09-24 21:59:38 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit1bf07cea4e38ba7c190d725fa887a139d229aef0 (patch)
tree53e2c1842898fadb9bf0aa5d0e9a5cd61a525d32 /examples/test.lisp
parent8831c89b582ee96fa4bf2078ad5a66302a6eeb66 (diff)
Add some example fns
Diffstat (limited to 'examples/test.lisp')
-rw-r--r--examples/test.lisp33
1 files changed, 22 insertions, 11 deletions
diff --git a/examples/test.lisp b/examples/test.lisp
index 536caa5..5f1dcf4 100644
--- a/examples/test.lisp
+++ b/examples/test.lisp
@@ -1,3 +1,4 @@
+;; map :: (a -> b) -> [a] -> [b]
(let map (\[f lst]
(match lst
[] []
@@ -5,13 +6,15 @@
(map (+ 1) [1 2 3])
+;; foldr :: (a -> b -> b) -> b -> [a] -> b
(let foldr (\[f accumulator lst]
(match lst
[] accumulator
- (f (foldr f accumulator (tail lst)) (head lst)))))
+ (f (head lst) (foldr f accumulator (tail lst))))))
(foldr + 0 [1 2 3])
+;; filter :: (a -> Bool) -> [a] -> [a]
(let filter (\[pred lst]
(match lst
[] []
@@ -27,16 +30,6 @@
(filter pred [0 1 2 3 4 5])
-;; (let fibo (\[n]
-;; (match n
-;; 0 0
-;; 1 1
-;; (+
-;; (fibo (- n 1))
-;; (fibo (- n 2))))))
-
-;; (fibo 10)
-
(let fibo (\[n]
(let lazy fibo-1 (fibo (- n 1)))
(let lazy fibo-2 (fibo (- n 2)))
@@ -49,3 +42,21 @@
(let mod (\[n k]
(- n (* k (/ n k)))))
+
+(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 [])))
+
+(reverse [1 2 3])
+
+(let compose (\[f g]
+ (\[x] (f (g x)))))
+
+((compose (+ 1) (+ 2)) 3)