diff options
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/aoc22_1.milch | 25 | ||||
| -rw-r--r-- | examples/effects-concept.milch | 4 | ||||
| -rw-r--r-- | examples/fibo.milch | 2 | ||||
| -rw-r--r-- | examples/import.milch | 4 | ||||
| -rw-r--r-- | examples/impure-concept.milch | 6 | ||||
| -rw-r--r-- | examples/purity-test.milch | 8 | ||||
| -rw-r--r-- | examples/records-concept.milch | 12 | ||||
| -rw-r--r-- | examples/test.milch | 32 | ||||
| -rw-r--r-- | examples/types-concept.milch | 10 |
9 files changed, 58 insertions, 45 deletions
diff --git a/examples/aoc22_1.milch b/examples/aoc22_1.milch index 4170661..7a4520a 100644 --- a/examples/aoc22_1.milch +++ b/examples/aoc22_1.milch @@ -1,23 +1,23 @@ -(import! "stdlib/common.milch") +(import "stdlib/common.milch") ;; utils ; convert ["a" "b" "c"] into "abc" -(let! str-from-vec (\[vec] +(let str-from-vec (\[vec] (foldr concat "" vec))) ;; solution -(let! sample-path "examples/aoc22_1.txt") -(let! sample-input (read-file! sample-path)) +(let sample-path "examples/aoc22_1.txt") +(let sample-input (read-file! sample-path)) (print-fmt! "Read {0} characters from \"{1}\"\n" [ (len sample-input) sample-path]) -(let! sample-vec (to-vec sample-input)) +(let sample-vec (to-vec sample-input)) -(let! result1 (pipe sample-vec [ +(let result1 (pipe sample-vec [ (split-by "\n") (map str-from-vec) (split-by "") @@ -27,3 +27,16 @@ ])) (print-fmt! "Result 1: {0}\n" [result1]) + +(let result2 (pipe sample-vec [ + (split-by "\n") + (map str-from-vec) + (split-by "") + (map (map parse-int)) + (map (foldr + 0)) + (sort-by (* -1)) + (take 3) + (foldr + 0) +])) + +(print-fmt! "Result 2: {0}\n" [result2]) diff --git a/examples/effects-concept.milch b/examples/effects-concept.milch index 5ea90d7..0bde72f 100644 --- a/examples/effects-concept.milch +++ b/examples/effects-concept.milch @@ -1,7 +1,7 @@ ; FUNCTIONS -(let! prompt-input (\[] - (let! p "> ") +(let prompt-input (\[] + (let p "> ") (do get-user-input "prompted" p))) ; SIGNAL HANDLERS diff --git a/examples/fibo.milch b/examples/fibo.milch index 26c74a1..c08fdd4 100644 --- a/examples/fibo.milch +++ b/examples/fibo.milch @@ -1,4 +1,4 @@ -(let! fibo (\[n] +(let fibo (\[n] (match n 0 0 1 1 diff --git a/examples/import.milch b/examples/import.milch index e839ef0..47a7477 100644 --- a/examples/import.milch +++ b/examples/import.milch @@ -1,2 +1,2 @@ -(import! "stdlib/common.milch") -(import! M "stdlib/maybe.milch") +(import "stdlib/common.milch") +(import M "stdlib/maybe.milch") diff --git a/examples/impure-concept.milch b/examples/impure-concept.milch index 893b29e..c767914 100644 --- a/examples/impure-concept.milch +++ b/examples/impure-concept.milch @@ -3,10 +3,10 @@ ; Pure functions can only call pure functions. ; Impure function names end in an exclamation mark (!) by convention. ; The top-level context is impure but has special rules -; related to `import!`, `let!`, and other builtins. +; related to `import`, `let`, and other builtins. ; pure function -(let! f (\[x] x)) +(let f (\[x] x)) ; impure function -(let! g! (\![x] (print! x))) +(let g! (\![x] (print! x))) diff --git a/examples/purity-test.milch b/examples/purity-test.milch index 6e8faea..f7c77e5 100644 --- a/examples/purity-test.milch +++ b/examples/purity-test.milch @@ -1,22 +1,22 @@ ; utils -(let! println! (\![x] +(let println! (\![x] (print! (fmt "{0}\n" [x])))) ; pure vs. impure demo ; this is a pure function -(let! f (\[x] x)) +(let f (\[x] x)) (f 10) ; works ; this is an impure function -(let! g! (\![x] (println! x))) +(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))) +(let h (\[x] (g! x))) (h "bar") ; error: cannot call impure function in pure context diff --git a/examples/records-concept.milch b/examples/records-concept.milch index 420fa9f..1686d75 100644 --- a/examples/records-concept.milch +++ b/examples/records-concept.milch @@ -1,16 +1,16 @@ -; Records are named tuples with an identifier. `record!` is a builtin function +; Records are named tuples with an identifier. `record` is a builtin function ; that generates functions for creating, reading and modifying a record. ; In type system terms, records are intersections. -; `record!` can only be called in the top-level context. +; `record` can only be called in the top-level context. -(record! Ns/User +(record Ns/User name phone-number) ; Generates the following functions: -(let! user +(let user (Ns/User/create "John" "010-123-456")) ; user ~> (Ns/User name:"John" phone-number:"010-123-456") @@ -36,8 +36,8 @@ ; 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) +(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 diff --git a/examples/test.milch b/examples/test.milch index 84193eb..fc41266 100644 --- a/examples/test.milch +++ b/examples/test.milch @@ -1,27 +1,27 @@ -(let! compose (\[f g] +(let compose (\[f g] (\[x] (f (g x))))) ((compose (+ 1) (+ 2)) 3) -(let! id (\[a] a)) +(let id (\[a] a)) -(let! mod (\[n k] +(let mod (\[n k] (- n (* k (/ n k))))) -(let! not (\[b] +(let not (\[b] (match b true false false true))) -(let! is-even (\[n] +(let is-even (\[n] (match (mod n 2) 0 true 1 false))) -(let! is-odd (compose not is-even)) +(let is-odd (compose not is-even)) ;; map :: (a -> b) -> [a] -> [b] -(let! map (\[f lst] +(let map (\[f lst] (match lst [] [] @@ -31,7 +31,7 @@ (map (+ 1) [1 2 3]) ;; foldr :: (a -> b -> b) -> b -> [a] -> b -(let! foldr (\[f accumulator lst] +(let foldr (\[f accumulator lst] (match lst [] accumulator @@ -41,7 +41,7 @@ (foldr + 0 [1 2 3]) ;; filter :: (a -> Bool) -> [a] -> [a] -(let! filter (\[pred lst] +(let filter (\[pred lst] (match lst [] [] otherwise (match (pred (head lst)) @@ -52,20 +52,20 @@ (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)) +(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] +(let reverse (\[v] (reverse_ v []))) (reverse [1 2 3]) -(let! flow (\[fs] (foldr compose id (reverse fs)))) -(let! pipe (\[x fs] ((flow fs) x))) +(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 8359c6b..9496ffe 100644 --- a/examples/types-concept.milch +++ b/examples/types-concept.milch @@ -5,26 +5,26 @@ (\[b] c) ; f (\[a] b)] ; g (\[a] c))) -(let! +(let compose (\[f g] (\[x] (f (g x))))) ((compose (+ 1) (+ 2)) 3) (type id (\[a] a)) -(let! id (\[a] a)) +(let id (\[a] a)) (type mod (\[Int Int] Int)) -(let! mod (\[n k] +(let mod (\[n k] (- n (* k (/ n k))))) (type not (\[Bool] Bool)) -(let! not (\[b] +(let not (\[b] (match b true false false true))) (type is-even (\[Int] Bool)) -(let! is-even (\[n] +(let is-even (\[n] (match (mod n 2) 0 true 1 false))) |
