From 1bf07cea4e38ba7c190d725fa887a139d229aef0 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sat, 24 Sep 2022 21:59:38 +0300 Subject: Add some example fns --- examples/test.lisp | 33 ++++++++++++++++++++++----------- 1 file 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) -- cgit v1.3