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 | // Known trait evidence (issue #61): natively, calls through constant |
| 2 | // dictionaries are direct and constrained functions get copies per |
| 3 | // dictionary. These shapes must compute what the interpreter computes. |
| 4 | |
| 5 | import std/dict |
| 6 | import std/io |
| 7 | import std/list |
| 8 | |
| 9 | // Polymorphic recursion: a call of itself with another dictionary. |
| 10 | fn depth :: x: a n: Int -> Int where Num<a> |
| 11 | { |
| 12 | if n == 0 |
| 13 | { |
| 14 | 0 |
| 15 | } |
| 16 | else |
| 17 | { |
| 18 | 1 + depth n (n - 1) |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | // The dictionary passed through two levels, at two types. |
| 23 | fn biggest :: xs: List<a> best: a -> a where Ord<a> |
| 24 | { |
| 25 | match xs { [] -> best, [x, ..rest] -> biggest rest (larger x best) } |
| 26 | } |
| 27 | |
| 28 | fn larger :: a: a b: a -> a where Ord<a> |
| 29 | { |
| 30 | if a > b |
| 31 | { |
| 32 | a |
| 33 | } |
| 34 | else |
| 35 | { |
| 36 | b |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | // A dictionary method used as a value (a closure), and a sum through Num. |
| 41 | fn sum_shown :: xs: List<a> -> String where Num<a>, Show<a> |
| 42 | { |
| 43 | string_of (list.map xs show) <> "=" <> show (list.sum xs) |
| 44 | } |
| 45 | |
| 46 | fn string_of :: parts: List<String> -> String |
| 47 | { |
| 48 | match parts { [] -> "", [p] -> p, [p, ..rest] -> p <> "+" <> string_of rest } |
| 49 | } |
| 50 | |
| 51 | // A derived dictionary (with a constraint) is not constant: left alone. |
| 52 | fn same :: a: List<a> b: List<a> -> Bool where Eq<a> |
| 53 | { |
| 54 | a == b |
| 55 | } |
| 56 | |
| 57 | pub fn main :: -> () / <io> |
| 58 | { |
| 59 | io.println (show (depth 1.5 5) <> " " <> show (depth 3 2)) |
| 60 | io.println (show (biggest [3, 9, 2] 0) <> " " <> biggest ["b", "z", "a"] "") |
| 61 | io.println (sum_shown [1, 2, 3] <> " " <> sum_shown [0.5, 0.25]) |
| 62 | io.println (show (same [1, 2] [1, 2]) <> " " <> show (same ["a"] ["b"])) |
| 63 | let d = dict.insert (dict.insert (dict.empty ()) "k" 1) "j" 2 |
| 64 | io.println (show (dict.size d) <> " " <> show (dict.get d "j")) |
| 65 | } |
| 66 | //> 5 2 |
| 67 | //> 9 z |
| 68 | //> 1+2+3=6 0.5+0.25=0.75 |
| 69 | //> True False |
| 70 | //> 2 Some(2) |