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 | // Tail recursion modulo cons (ADR 0039) whose cells hold nothing but |
| 2 | // constants besides the hole (issue #331): `Succ(…)`, `[0, ..…]`, |
| 3 | // `Link(7, …)`, and a constant second cell after a reused first one. The |
| 4 | // hole is unit until the context fills it, so such a cell looked like a |
| 5 | // constant tree and was emitted as a static, read-only block, which the |
| 6 | // context then wrote into. 30,000 deep, in constant stack. |
| 7 | //@ small-stack |
| 8 | |
| 9 | import std/io |
| 10 | import std/list |
| 11 | import std/string |
| 12 | |
| 13 | type Peano { Zero, Succ(prev: Peano) } |
| 14 | |
| 15 | fn peano :: n: Int -> Peano |
| 16 | { |
| 17 | if n == 0 |
| 18 | { |
| 19 | Zero |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | Succ(peano (n - 1)) |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | fn back :: p: Peano acc: Int -> Int |
| 28 | { |
| 29 | match p { Zero -> acc, Succ(prev) -> back prev (acc + 1) } |
| 30 | } |
| 31 | |
| 32 | fn zeros :: n: Int -> List<Int> |
| 33 | { |
| 34 | if n == 0 |
| 35 | { |
| 36 | [] |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | [0, ..zeros (n - 1)] |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | type Chain { End, Link(n: Int, next: Chain) } |
| 45 | |
| 46 | fn sevens :: n: Int -> Chain |
| 47 | { |
| 48 | if n == 0 |
| 49 | { |
| 50 | End |
| 51 | } |
| 52 | else |
| 53 | { |
| 54 | Link(7, sevens (n - 1)) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn sum :: c: Chain acc: Int -> Int |
| 59 | { |
| 60 | match c { End -> acc, Link(n, next) -> sum next (acc + n) } |
| 61 | } |
| 62 | |
| 63 | fn spaced :: words: List<String> -> List<String> |
| 64 | { |
| 65 | match words { [] -> [], [word, ..rest] -> [word, ",", ..spaced rest] } |
| 66 | } |
| 67 | |
| 68 | pub fn main :: -> () / <io> |
| 69 | { |
| 70 | let n = 30000 |
| 71 | io.println "{back (peano n) 0}" |
| 72 | io.println "{list.length (zeros n)} {zeros 2}" |
| 73 | io.println "{sum (sevens n) 0}" |
| 74 | io.println (string.concat (spaced ["a", "b", "c"])) |
| 75 | io.println "{list.length (spaced (list.map (list.range 1 n) fn i { "{i}" }))}" |
| 76 | // Built twice: no evaluation shares a cell with another. |
| 77 | io.println "{zeros 3} {zeros 3}" |
| 78 | } |
| 79 | //> 30000 |
| 80 | //> 30000 [0, 0] |
| 81 | //> 210000 |
| 82 | //> a,b,c, |
| 83 | //> 60000 |
| 84 | //> [0, 0, 0] [0, 0, 0] |