diff options
| author | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-06 19:43:01 +0200 |
|---|---|---|
| committer | Jan Tuomi <jans.tuomi@gmail.com> | 2022-12-06 19:43:01 +0200 |
| commit | 35bf45fe54788c6fca6b105b65fc1622a4bb7d34 (patch) | |
| tree | f89b430ff4b143ce5ce8780210b0151b8b560187 /core | |
| parent | 82562a35e8f0706a812e3aefa8e837b88a1a0d95 (diff) | |
Rename and add lib stuff
Diffstat (limited to 'core')
| -rw-r--r-- | core/common.milch | 176 |
1 files changed, 111 insertions, 65 deletions
diff --git a/core/common.milch b/core/common.milch index f05c6a5..55fa2b8 100644 --- a/core/common.milch +++ b/core/common.milch @@ -1,87 +1,131 @@ +;; Control flow +;;;;;;;;;;;;;;;;;;;;;;;;; + (let compose (\[f g] (\[x] (f (g x))))) +(let flow (\[fs] + (foldr compose id (reverse fs)))) + +(let pipe (\[x fs] + ((flow fs) x))) + +;; Standard functions +;;;;;;;;;;;;;;;;;;;;;;;;; + (let id (\[a] a)) -(let mod (\[n k] - (- n (* k (/ n k))))) +;; Logic +;;;;;;;;;;;;;;;;;;;;;;;;; (let not (\[b] (match b true false false true))) -(let is-even (\[n] +(let and (\[a b] + (match a + true b + false (not b)))) + +(let or (\[a b] + (match a + true true + false b))) + +(let xor (\[a b] + (match a + true (not b) + false b))) + +;; Math +;;;;;;;;;;;;;;;;;;;;;;;;; + +(let PI 3.141592653589793238) +(let E 2.718281828459045235) + +(let mod (\[n k] + (- n (* k (/ n k))))) + +(let even? (\[n] (match (mod n 2) 0 true 1 false))) -(let is-odd (compose not is-even)) +(let odd? (compose not even?)) + +(let leq? (\[a b] + (or? + (eq? a b) + (lt? a b)))) + +(let rt? (compose not leq?)) +(let req? (compose not lt?)) + +(let max (\[a b] + (match (lt? a b) + true b + false a))) + +(let min (\[a b] + (match (lt? a b) + true a + false b))) + +;; Vector operations +;;;;;;;;;;;;;;;;;;;;;;;;; + +(let sort-by (\[keyf vals] + (let sorted (sort-by-first + (map (\[v] + [(keyf v) v]) vals))) + + (map (at 1) sorted))) + +(let take (\[n xs] + (match n + 0 [] + otherwise (prepend (head xs) (take (- n 1) (tail xs)))))) + +(let drop (\[n xs] + (match n + 0 xs + otherwise (drop (- n 1) (tail xs))))) -;; map :: (a -> b) -> [a] -> [b] +; map :: (a -> b) -> [a] -> [b] (let map (\[f lst] (match lst - [] - [] - otherwise - (prepend (f (head lst)) (map f (tail lst)))))) + [] [] + otherwise (prepend (f (head lst)) (map f (tail lst)))))) -; (map (+ 1) [1 2 3]) - -;; foldr :: (a -> b -> b) -> b -> [a] -> b +; foldr :: (a -> b -> b) -> b -> [a] -> b (let foldr (\[f accumulator lst] (match lst - [] - accumulator - otherwise - (f (head lst) (foldr f accumulator (tail lst)))))) + [] accumulator + otherwise (f (head lst) (foldr f accumulator (tail lst)))))) -;; filter :: (a -> Bool) -> [a] -> [a] +; filter :: (a -> Bool) -> [a] -> [a] (let filter (\[pred lst] (match lst - [] [] - otherwise (match (pred (head lst)) + [] [] + otherwise (match (pred (head lst)) true (prepend (head lst) (filter pred (tail lst))) false (filter pred (tail lst)))))) -(let reverse_ (\[v a] +(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)))) + _ (_reverse xs xa)))) (let reverse (\[v] - (reverse_ v []))) - -(let flow (\[fs] (foldr compose id (reverse fs)))) -(let pipe (\[x fs] ((flow fs) x))) - -(let leq? (\[a b] - (or? - (eq? a b) - (lt? a b)))) - -(let rt? (compose not leq?)) -(let req? (compose not lt?)) + (_reverse v []))) -(let max2 (\[a b] - (match (lt? a b) - true b - false a))) - -(let max (\[vals] - (let lazy v (head vals)) - (let vs (tail vals)) - - (match vs - [] v - otherwise (max2 v (max vs))))) - -(let split-by' (\[delim acc vals] +(let _split-by (\[delim acc vals] (let lazy v (head vals)) (let vs (tail vals)) @@ -90,12 +134,12 @@ delim [(reverse acc)] otherwise [(prepend v (reverse acc))]) otherwise (match v - delim (prepend (reverse acc) (split-by' delim [] vs)) - otherwise (split-by' delim (prepend v acc) vs))))) + delim (prepend (reverse acc) (_split-by delim [] vs)) + otherwise (_split-by delim (prepend v acc) vs))))) ; split vector by delimiter (let split-by (\[delim vals] - (split-by' delim [] vals))) + (_split-by delim [] vals))) (let at (\[n seq] (match seq @@ -104,22 +148,24 @@ 0 (head seq) otherwise (at (- n 1) (tail seq)))))) -(let print-fmt! (\![fstr args] - (print! (fmt fstr args)))) +(let maximum (\[vals] + (let lazy v (head vals)) + (let vs (tail vals)) -(let sort-by (\[keyf vals] - (let sorted (sort-by-first - (map (\[v] - [(keyf v) v]) vals))) + (match vs + [] v + otherwise (max v (maximum vs))))) - (map (at 1) sorted))) +(let minimum (\[vals] + (let lazy v (head vals)) + (let vs (tail vals)) -(let take (\[n xs] - (match n - 0 [] - otherwise (prepend (head xs) (take (- n 1) (tail xs)))))) + (match vs + [] v + otherwise (min v (minimum vs))))) -(let drop (\[n xs] - (match n - 0 xs - otherwise (drop (- n 1) (tail xs))))) +;; IO +;;;;;;;;;;;;;;;;;;;;;;;;; + +(let print-fmt! (\![fstr args] + (print! (fmt fstr args)))) |
