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 | // A multi-shot resumption taken in the middle of a `map` (ADR 0039, ADR |
| 2 | // 0017): each resumption builds its own list, so a context both share is |
| 3 | // copied before it changes. Same lists, in the same order, on both |
| 4 | // backends. |
| 5 | |
| 6 | import std/io |
| 7 | import std/list |
| 8 | |
| 9 | effect amb |
| 10 | { |
| 11 | multi ctl choose :: -> Bool |
| 12 | } |
| 13 | |
| 14 | fn all :: action: fn() -> a / <amb | e> -> List<a> / <e> |
| 15 | { |
| 16 | handle |
| 17 | { |
| 18 | [action ()] |
| 19 | } |
| 20 | with |
| 21 | { choose -> list.append (resume True) (resume False) } |
| 22 | } |
| 23 | |
| 24 | /// Each element doubled or not: 2^3 lists, built by `map`. |
| 25 | fn choices :: xs: List<Int> -> List<List<Int>> |
| 26 | { |
| 27 | all |
| 28 | fn |
| 29 | { |
| 30 | list.map |
| 31 | xs |
| 32 | fn x |
| 33 | { |
| 34 | if choose () |
| 35 | { |
| 36 | x * 2 |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | x |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | /// The same, with a `map` inside a `map`'s mapper. |
| 47 | fn nested :: -> List<List<List<Int>>> |
| 48 | { |
| 49 | all |
| 50 | fn |
| 51 | { |
| 52 | list.map |
| 53 | [1, 2] |
| 54 | fn x |
| 55 | { |
| 56 | list.map |
| 57 | [10, 20] |
| 58 | fn y |
| 59 | { |
| 60 | if choose () |
| 61 | { |
| 62 | x + y |
| 63 | } |
| 64 | else |
| 65 | { |
| 66 | 0 |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | pub fn main :: -> () / <io> |
| 74 | { |
| 75 | list.each (choices [1, 2, 3]) fn l { io.println "{l}" } |
| 76 | let n = nested () |
| 77 | match (n, list.reverse n) |
| 78 | { ([first, ..], [last, ..]) -> io.println "{list.length n} {first} {last}" |
| 79 | , _ -> io.println "none" |
| 80 | } |
| 81 | } |
| 82 | //> [2, 4, 6] |
| 83 | //> [2, 4, 3] |
| 84 | //> [2, 2, 6] |
| 85 | //> [2, 2, 3] |
| 86 | //> [1, 4, 6] |
| 87 | //> [1, 4, 3] |
| 88 | //> [1, 2, 6] |
| 89 | //> [1, 2, 3] |
| 90 | //> 16 [[11, 21], [12, 22]] [[0, 0], [0, 0]] |