tobogganSe connecter

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.

64 lignes · 1032 octetsBrut · Historique
1import std/io
2
3trait Describe<a>
4{
5 fn describe :: x: a -> String
6}
7
8pub type Box<a> { Box(a) }
9
10impl Describe<Int>
11{
12 fn describe :: n
13 {
14 "int " <> show n
15 }
16}
17
18impl Describe<Box<a>> where Describe<a>
19{
20 fn describe :: b
21 {
22 match b { Box(x) -> "box of " <> describe x }
23 }
24}
25
26// Constraints inferred from use become dictionary parameters.
27fn twice_described :: x
28{
29 describe x <> " / " <> describe x
30}
31
32pub fn largest :: a: t b: t -> t where Ord<t>
33{
34 if a > b
35 {
36 a
37 }
38 else
39 {
40 b
41 }
42}
43
44fn sum_all :: xs
45{
46 match xs { [] -> 0, [x, ..rest] -> x + sum_all rest }
47}
48
49pub fn main :: -> () / <io>
50{
51 io.println (describe Box(Box(42)))
52 io.println (twice_described Box(7))
53 io.println
54 ( show (largest 3 9) <> " " <> largest "pear" "apple" <> " "
55 <> show (largest 2.5 (-1.0))
56 )
57 io.println (show ([1, 2] == [1, 2]) <> " " <> show (Some(3) == None))
58 io.println (show (sum_all [1, 2, 3, 4]))
59}
60//> box of box of int 42
61//> box of int 7 / box of int 7
62//> 9 pear 2.5
63//> True False
64//> 10