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 | // Constructed product results (issue #200, ADR 0043): a tuple-returning |
| 2 | // helper whose result a caller destructures goes through a worker that |
| 3 | // returns the components, so the tuple is never built on that path. |
| 4 | |
| 5 | import std/io |
| 6 | |
| 7 | // Deliberately branchy so the simplifier won't inline it (CPR's case, |
| 8 | // not inlining's). |
| 9 | fn splitmix :: x: Int -> (Int, Int) |
| 10 | { |
| 11 | if x == 0 |
| 12 | { |
| 13 | (0, 0) |
| 14 | } |
| 15 | else |
| 16 | { |
| 17 | if x == 1 |
| 18 | { |
| 19 | (1, 2) |
| 20 | } |
| 21 | else |
| 22 | { |
| 23 | if x == 2 |
| 24 | { |
| 25 | (3, 4) |
| 26 | } |
| 27 | else |
| 28 | { |
| 29 | if x == 3 |
| 30 | { |
| 31 | (5, 6) |
| 32 | } |
| 33 | else |
| 34 | { |
| 35 | if x == 4 |
| 36 | { |
| 37 | (7, 8) |
| 38 | } |
| 39 | else |
| 40 | { |
| 41 | (x * 3, x * 5) |
| 42 | } |
| 43 | } |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | fn go :: n: Int acc: Int -> Int |
| 50 | { |
| 51 | if n == 0 |
| 52 | { |
| 53 | acc |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | let (a, b) = splitmix (n % 7) |
| 58 | go (n - 1) (acc + a + b) |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn main :: -> () / <io> |
| 63 | { |
| 64 | io.println (show (go 1000 0)) |
| 65 | // Kept whole, not destructured: the wrapper path still builds a tuple. |
| 66 | let whole = splitmix 6 |
| 67 | io.println "whole: {whole:?}" |
| 68 | } |
| 69 | //> 17732 |
| 70 | //> whole: (18, 30) |