louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | import std/io |
| 2 | import std/list |
| 3 | |
| 4 | // Pipes and captures (ADR 0023): `x |> f(a)` is `f(x, a)`, |
| 5 | // `x |> f(a, _)` is `f(a, x)`, and `f(a, _)` alone is `fn(v) { f(a, v) }`. |
| 6 | |
| 7 | fn sub :: x: Int y: Int -> Int |
| 8 | { |
| 9 | x - y |
| 10 | } |
| 11 | |
| 12 | fn greet :: ~greeting: String ~name: String -> String |
| 13 | { |
| 14 | greeting <> ", " <> name |
| 15 | } |
| 16 | |
| 17 | /// Prints `label` and returns `x`: shows the evaluation order. |
| 18 | fn trace :: label: String x: a -> a / <io> |
| 19 | { |
| 20 | io.println label |
| 21 | x |
| 22 | } |
| 23 | |
| 24 | fn sum_to :: n: Int acc: Int -> Int |
| 25 | { |
| 26 | // A pipe in tail position stays a tail call. |
| 27 | if n == 0 |
| 28 | { |
| 29 | acc |
| 30 | } |
| 31 | else |
| 32 | { |
| 33 | acc + n |> sum_to (n - 1) _ |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | fn subtract_from :: x: Int |
| 38 | { |
| 39 | fn y { x - y } |
| 40 | } |
| 41 | |
| 42 | fn pick :: |
| 43 | { |
| 44 | io.println "callee" |
| 45 | sub |
| 46 | } |
| 47 | |
| 48 | pub fn main :: -> () / <io> |
| 49 | { |
| 50 | // Argument placing. |
| 51 | io.println (show (10 |> sub 3)) |
| 52 | io.println (show (10 |> sub 3 _)) |
| 53 | io.println (show (10 |> sub _ 3)) |
| 54 | io.println ("Ada" |> greet ~greeting: "Hello" ~name: _) |
| 55 | io.println (greet ~greeting: "Hi" ~name: "Grace") |
| 56 | io.println (show ([1, 2, 3] |> list.map (sub 10 _) |> list.sum)) |
| 57 | |
| 58 | // The left side is evaluated first, as written. |
| 59 | let r = trace "left" 5 |> sub (trace "argument" 1) _ |
| 60 | io.println (show r) |
| 61 | let r = trace "left" 5 |> (pick ()) 1 |
| 62 | io.println (show r) |
| 63 | // A function returned by a call: place the value explicitly. |
| 64 | let r = trace "left" 5 |> (subtract_from (trace "argument" 1)) _ |
| 65 | io.println (show r) |
| 66 | |
| 67 | // Captures: the other arguments are evaluated at each call. |
| 68 | let minus = sub (trace "captured" 100) _ |
| 69 | io.println "made" |
| 70 | io.println (show (minus 1 + minus 2)) |
| 71 | list.each ["x", "y"] (io.println _) |
| 72 | io.println (show (list.map [1, 2] Some(_))) |
| 73 | io.println (show (sum_to 1000000 0)) |
| 74 | } |
| 75 | //> 7 |
| 76 | //> -7 |
| 77 | //> 7 |
| 78 | //> Hello, Ada |
| 79 | //> Hi, Grace |
| 80 | //> 24 |
| 81 | //> left |
| 82 | //> argument |
| 83 | //> -4 |
| 84 | //> left |
| 85 | //> callee |
| 86 | //> 4 |
| 87 | //> left |
| 88 | //> argument |
| 89 | //> -4 |
| 90 | //> made |
| 91 | //> captured |
| 92 | //> captured |
| 93 | //> 197 |
| 94 | //> x |
| 95 | //> y |
| 96 | //> [Some(1), Some(2)] |
| 97 | //> 500000500000 |