diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/effects-concept.milch | 3 | ||||
| -rw-r--r-- | examples/import.milch | 2 | ||||
| -rw-r--r-- | examples/impure-concept.milch | 19 | ||||
| -rw-r--r-- | examples/purity-test.milch | 25 | ||||
| -rw-r--r-- | examples/records-concept.milch | 24 | ||||
| -rw-r--r-- | examples/test.milch | 62 | ||||
| -rw-r--r-- | examples/types-concept.milch | 3 |
7 files changed, 23 insertions, 115 deletions
diff --git a/examples/effects-concept.milch b/examples/effects-concept.milch index 0bde72f..6091945 100644 --- a/examples/effects-concept.milch +++ b/examples/effects-concept.milch @@ -1,3 +1,6 @@ +; Probably not going to implement this, +; just writing down ideas + ; FUNCTIONS (let prompt-input (\[] diff --git a/examples/import.milch b/examples/import.milch deleted file mode 100644 index 47a7477..0000000 --- a/examples/import.milch +++ /dev/null @@ -1,2 +0,0 @@ -(import "stdlib/common.milch") -(import M "stdlib/maybe.milch") diff --git a/examples/impure-concept.milch b/examples/impure-concept.milch index c767914..75c3bb7 100644 --- a/examples/impure-concept.milch +++ b/examples/impure-concept.milch @@ -5,8 +5,21 @@ ; The top-level context is impure but has special rules ; related to `import`, `let`, and other builtins. -; pure function +; this is a pure function (let f (\[x] x)) -; impure function -(let g! (\![x] (print! x))) +(f 10) ; works + +; this is an impure function +(let g! (\![x] (println! x))) + +(g! "foo") ; works + +; this is a pure function that tries to call an impure function +(let h (\[x] (g! x))) + +(h "bar") +; error: cannot call impure function in pure context +; when calling function g! at examples/purity-test.milch:19:15 +; in a function definition at examples/purity-test.milch:19:12 +; when calling function h at examples/purity-test.milch:21:1
\ No newline at end of file diff --git a/examples/purity-test.milch b/examples/purity-test.milch deleted file mode 100644 index f7c77e5..0000000 --- a/examples/purity-test.milch +++ /dev/null @@ -1,25 +0,0 @@ -; utils - -(let println! (\![x] - (print! (fmt "{0}\n" [x])))) - -; pure vs. impure demo - -; this is a pure function -(let f (\[x] x)) - -(f 10) ; works - -; this is an impure function -(let g! (\![x] (println! x))) - -(g! "foo") ; works - -; this is a pure function that tries to call an impure function -(let h (\[x] (g! x))) - -(h "bar") -; error: cannot call impure function in pure context -; when calling function g! at examples/purity-test.milch:19:15 -; in a function definition at examples/purity-test.milch:19:12 -; when calling function h at examples/purity-test.milch:21:1 diff --git a/examples/records-concept.milch b/examples/records-concept.milch index 1686d75..dcb4a49 100644 --- a/examples/records-concept.milch +++ b/examples/records-concept.milch @@ -22,30 +22,6 @@ (Ns/User/set-phone-number unit user) ; => (Ns/User name:"Rick" phone-number:<unit>) - - - -; Unions are unions of records with an identifier. `union!` is a builtin function -; that generates functions for manipulating the records in the union. - -; `union!` can only be called in the top-level context. - -(union! Ns/Maybe - (just value) - (nothing)) - -; Generates the following functions: - -(let m1 (Ns/Maybe/just 10)) ; m1 ~> (Ns/Maybe/Just value:10) -(let m2 (Ns/Maybe/nothing)) ; m2 ~> (Ns/Maybe/Nothing) - -(Ns/Maybe/Just/get-value m1) ; => 10 - - - - ; The builtin function `kind` returns the identifier as a string for any record value -(kind m1) ; => "Ns/Maybe/Just" -(kind m2) ; => "Ns/Maybe/Nothing" (kind user) ; => "Ns/User" diff --git a/examples/test.milch b/examples/test.milch index fc41266..d8d0077 100644 --- a/examples/test.milch +++ b/examples/test.milch @@ -1,71 +1,11 @@ -(let compose (\[f g] - (\[x] (f (g x))))) - -((compose (+ 1) (+ 2)) 3) - -(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)))))) +(import "stdlib/common.milch") (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)))))) - (foldr + 0 [1 2 3]) -;; 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)))))) - (filter is-even [0 1 2 3 4 5]) -(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 flow (\[fs] (foldr compose id (reverse fs)))) -(let pipe (\[x fs] ((flow fs) x))) - (pipe 10 [(+ 1) (+ 2)]) diff --git a/examples/types-concept.milch b/examples/types-concept.milch index 9496ffe..476c96b 100644 --- a/examples/types-concept.milch +++ b/examples/types-concept.milch @@ -1,3 +1,6 @@ +; Probably not going to implement this, +; just writing down ideas + ; lowercase types: polymorphic ; everything else: monomorphic |
