import std/dict.{type Dict} import std/io import std/list import std/option // Perceus optimisations (issue #34): a matched cell that is unique is // freed or reused in place; a shared one is left intact. Parameters only // read are borrowed. Every result must be the same as without them. @derive Show pub type Tree { Leaf, Node(Tree, Int, Tree) } @derive Show pub type Shape { One(Int), Two(Int, Int), Three(Int, Int, Int), Flip(Int) } fn insert :: t x { match t { Leaf -> Node(Leaf, x, Leaf) , Node(l, v, r) -> if x < v { Node(insert l x, v, r) } else if x > v { Node(l, v, insert r x) } else { t } } } fn incr :: t { match t { Leaf -> Leaf, Node(l, v, r) -> Node(incr l, v + 1, incr r) } } fn total :: t { match t { Leaf -> 0, Node(l, v, r) -> total l + v + total r } } fn build :: xs { list.fold xs Leaf insert } // Reuse across sizes and constructors: the same size reuses the cell // (with a new tag); another size cannot. fn reshape :: s { match s { One(a) -> Flip(a) , Flip(a) -> One(a + 1) , Two(a, b) -> Three(a, b, a + b) , Three(a, b, c) -> Two(a + b, c) } } // A tuple matched without a `switch`. fn swap :: p { let (a, b) = p (b, a) } // Pairs neighbours: two cells matched, one built. fn pairs :: xs { match xs { [a, b, ..rest] -> [a * 10 + b, ..pairs rest], other -> other } } // A borrowed parameter whose field escapes into the result. fn label :: p suffix { let (name, _) = p (name <> suffix, name) } // A borrowed dictionary and a value read from a borrowed map. fn lookup_all :: d: Dict keys: List -> List { match keys { [] -> [] , [k, ..rest] -> { let found = match dict.get d k { Some(v) -> v, None -> "-" } [found, ..lookup_all d rest] } } } // Generic arithmetic: the `Num` dictionary is passed along unchanged. fn sum_to :: n: a i: a step: a acc: a -> a where Num, Ord { if i > n { acc } else { sum_to n (i + step) step (acc + i) } } pub fn main :: -> () / { let xs = list.range 1 10 // Unique: map, filter and reverse reuse the cells of their input. io.println ( show ( list.reverse ( list.filter (list.map (list.range 1 10) fn x { x * x }) fn x { x % 2 == 1 } ) ) ) // Shared: `xs` is used again, so its cells must not be overwritten. let doubled = list.map xs fn x { x * 2 } io.println (show doubled <> " from " <> show xs) io.println (show (pairs xs) <> " " <> show (pairs [1, 2, 3])) let t = build [5, 2, 8, 1, 9, 3] let u = incr t io.println ( show (total t) <> " " <> show (total u) <> " " <> show (total (incr (build [4, 4, 6]))) ) io.println (show t) io.println (show (list.map [One(1), Two(2, 3), Three(4, 5, 6), Flip(7)] reshape)) let shapes = [One(1), Two(2, 3)] let again = list.map shapes reshape io.println (show again <> " from " <> show shapes) let p = (1, "one") let q = swap p let r = swap (swap p) io.println (q.0 <> show q.1 <> " " <> show r.0 <> r.1 <> " " <> show p.0 <> p.1) let named = ("ada", 36) let (long, short) = label named "!" io.println (long <> " " <> short <> " " <> show named.1) let d = dict.from_list [(1, "one"), (2, "two"), (3, "three")] io.println (show (lookup_all d [3, 4, 1]) <> " " <> show (dict.size d)) io.println (show (sum_to 100 0 1 0) <> " " <> show (sum_to 2.0 0.0 0.5 0.0)) } //> [81, 49, 25, 9, 1] //> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20] from [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] //> [12, 34, 56, 78, 100] [12, 3] //> 28 34 12 //> Node(Node(Node(Leaf, 1, Leaf), 2, Node(Leaf, 3, Leaf)), 5, Node(Leaf, 8, Node(Leaf, 9, Leaf))) //> [Flip(1), Three(2, 3, 5), Two(9, 6), One(8)] //> [Flip(1), Three(2, 3, 5)] from [One(1), Two(2, 3)] //> one1 1one 1one //> ada! ada 36 //> [three, -, one] 3 //> 5050 5.0