From 4e94267a9f97865ebeb19bcbe4945322dd74a1b3 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 24 Sep 2022 19:23:40 +0300 Subject: Add nice example code in test.lisp --- examples/test.lisp | 58 +++++++++++++++++++++++++++++------------------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/examples/test.lisp b/examples/test.lisp index 9930181..6608fae 100644 --- a/examples/test.lisp +++ b/examples/test.lisp @@ -1,30 +1,9 @@ -;; (let map (\[f lst] -;; (match lst -;; [] [] -;; (prepend (f (head lst)) (map f (tail lst)))))) - -;; (map (+ 1) [1 2 3]) - -;; (let mapinc (\[vec] -;; (match vec -;; [] [] -;; (prepend -;; (+ 1 (head vec)) -;; (mapinc (tail vec)))))) -;; (env) -;; (mapinc [1 2 3]) - -;; (let fibo (\[n] -;; ;; (let fibo-1 (\[] (fibo (sub2 n 1)))) -;; ;; (let fibo-2 (\[] (fibo (sub2 n 2)))) -;; (match n -;; 0 0 -;; 1 1 -;; (+ -;; (fibo (- n 1)) -;; (fibo (- n 2)))))) - -;; (fibo 10) +(let map (\[f lst] + (match lst + [] [] + (prepend (f (head lst)) (map f (tail lst)))))) + +(map (+ 1) [1 2 3]) (let foldr (\[f accumulator lst] (match lst @@ -32,3 +11,28 @@ (f (foldr f accumulator (tail lst)) (head lst))))) (foldr + 0 [1 2 3]) + +(let filter (\[pred lst] + (match lst + [] [] + (match (pred (head lst)) + true (prepend (head lst) (filter pred (tail lst))) + false (filter pred (tail lst)))))) + +(let pred (\[n] + (match n + 2 true + 4 true + false))) + +(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) -- cgit v1.3