mirror of
https://github.com/anoma/juvix.git
synced 2025-01-07 08:08:44 +03:00
2baad15a41
* Enables new function syntax in local let-declarations * Closes #2251
59 lines
1.2 KiB
Plaintext
59 lines
1.2 KiB
Plaintext
-- pattern matching
|
|
module test035;
|
|
|
|
import Stdlib.Prelude open;
|
|
|
|
lgen : Nat → List Nat
|
|
| zero := nil
|
|
| (suc n) := suc n :: lgen n;
|
|
|
|
sum2 : List Nat → List Nat
|
|
| (x :: y :: xs) := x + y :: sum2 (y :: xs)
|
|
| xs := xs;
|
|
|
|
type Tree :=
|
|
| leaf : Tree
|
|
| node : Tree -> Tree -> Tree;
|
|
|
|
gen : Nat → Tree
|
|
| zero := leaf
|
|
| (suc zero) := node leaf leaf
|
|
| (suc (suc n)) := node (gen n) (gen (suc n));
|
|
|
|
terminating
|
|
f : Tree → Nat
|
|
| leaf := 1
|
|
| (node l r) :=
|
|
case g l, g r
|
|
| leaf, leaf := 3
|
|
| node l r, leaf := mod ((f l + f r) * 2) 20000
|
|
| node l1 r1, node l2 r2 :=
|
|
mod ((f l1 + f r1) * (f l2 + f r2)) 20000
|
|
| _, node l r := mod (f l + f r) 20000;
|
|
|
|
g : Tree → Tree
|
|
| leaf := leaf
|
|
| (node (node _ _) r) := r
|
|
| (node l r) := node r l;
|
|
|
|
h : Nat -> Nat
|
|
| (suc (suc (suc (suc n)))) := n
|
|
| _ := 0;
|
|
|
|
printListNatLn : List Nat → IO
|
|
| nil := printStringLn "nil"
|
|
| (x :: xs) :=
|
|
printNat x >> printString " :: " >> printListNatLn xs;
|
|
|
|
main : IO :=
|
|
printListNatLn (sum2 (lgen 5))
|
|
>> printNatLn (f (gen 10))
|
|
>> printNatLn (f (gen 15))
|
|
>> printNatLn (f (gen 16))
|
|
>> printNatLn (f (gen 17))
|
|
>> printNatLn (f (gen 18))
|
|
>> printNatLn (f (gen 20))
|
|
>> printNatLn (h 5)
|
|
>> printNatLn (h 3);
|
|
|