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.

70 lignes · 1705 octetsBrut · Historique
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
5import std/dict
6import std/io
7import std/list
8
9// Polymorphic recursion: a call of itself with another dictionary.
10fn 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.
23fn biggest :: xs: List<a> best: a -> a where Ord<a>
24{
25 match xs { [] -> best, [x, ..rest] -> biggest rest (larger x best) }
26}
27
28fn 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.
41fn 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
46fn 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.
52fn same :: a: List<a> b: List<a> -> Bool where Eq<a>
53{
54 a == b
55}
56
57pub 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)