
louise
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#1199 · main · 10 min 33 s
1Étoiles
0Forks
0Issues
| 1 | import std/io |
| 2 | |
| 3 | pub type User { User(name: String, age: Int) } |
| 4 | |
| 5 | @derive Eq Show Ord |
| 6 | pub type Shape { Circle(Int), Rect(Int, Int), Empty } |
| 7 | |
| 8 | fn area :: s |
| 9 | { |
| 10 | match s |
| 11 | { Circle(r) -> 3 * r * r |
| 12 | , Rect(w, h) if w == h -> w * w |
| 13 | , Rect(w, h) -> w * h |
| 14 | , Empty -> 0 |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | fn describe :: xs |
| 19 | { |
| 20 | match xs |
| 21 | { [] -> "empty" |
| 22 | , [x] -> "one: " <> show x |
| 23 | , [1, _] | [_, 1] -> "a pair with a one" |
| 24 | , [a, b, ..rest] -> |
| 25 | "starts " <> show a <> "," <> show b <> " then " <> show (length rest) |
| 26 | <> " more" |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | fn length :: xs |
| 31 | { |
| 32 | match xs { [] -> 0, [_, ..t] -> 1 + length t } |
| 33 | } |
| 34 | |
| 35 | fn classify :: n |
| 36 | { |
| 37 | match n |
| 38 | { 0 -> "zero", -1 -> "minus one", _ if n > 100 -> "big", _ -> "other" } |
| 39 | } |
| 40 | |
| 41 | pub fn main :: -> () / <io> |
| 42 | { |
| 43 | let ada = User(age: 36, name: "Ada") |
| 44 | let older = User(..ada, age: ada.age + 1) |
| 45 | io.println (older.name <> " is " <> show older.age) |
| 46 | io.println |
| 47 | ( show (area Circle(2)) <> " " <> show (area Rect(3, 3)) <> " " |
| 48 | <> show (area Rect(2, 5)) |
| 49 | <> " " |
| 50 | <> show (area Empty) |
| 51 | ) |
| 52 | io.println (describe []) |
| 53 | io.println (describe [7]) |
| 54 | io.println (describe [1, 9]) |
| 55 | io.println (describe [4, 5, 6, 7]) |
| 56 | io.println |
| 57 | ( classify 0 <> " " <> classify (-1) <> " " <> classify 500 <> " " |
| 58 | <> classify 7 |
| 59 | ) |
| 60 | io.println (show Rect(1, 2) <> " " <> show Empty) |
| 61 | io.println |
| 62 | (show (Rect(1, 2) == Rect(1, 2)) <> " " <> show (Circle(1) == Circle(2))) |
| 63 | io.println |
| 64 | ( show (Circle(9) < Rect(0, 0)) <> " " <> show (Rect(1, 5) < Rect(1, 2)) |
| 65 | <> " " |
| 66 | <> show (Empty > Circle(1)) |
| 67 | ) |
| 68 | let (x, y) = (10, 20) |
| 69 | let pair = ("left", (x, y)) |
| 70 | io.println (pair.0 <> " " <> show pair.1.1) |
| 71 | } |
| 72 | //> Ada is 37 |
| 73 | //> 12 9 10 0 |
| 74 | //> empty |
| 75 | //> one: 7 |
| 76 | //> a pair with a one |
| 77 | //> starts 4,5 then 2 more |
| 78 | //> zero minus one big other |
| 79 | //> Rect(1, 2) Empty |
| 80 | //> True False |
| 81 | //> True False True |
| 82 | //> left 20 |