diff --git a/12/examples/fixpoint.txt b/12/examples/fixpoint.txt new file mode 100644 index 0000000..aba81e8 --- /dev/null +++ b/12/examples/fixpoint.txt @@ -0,0 +1,17 @@ +data List a = { Nil, Cons a (List a) } + +defn fix f = { let { defn x = { f x } } in { x } } +defn fixpointOnes fo = { Cons 1 fo } +defn sumTwo l = { + case l of { + Nil -> { 0 } + Cons x xs -> { + x + case xs of { + Nil -> { 0 } + Cons y ys -> { y } + } + } + } +} + +defn main = { sumTwo (fix fixpointOnes) } diff --git a/12/examples/lambda.txt b/12/examples/lambda.txt new file mode 100644 index 0000000..35deace --- /dev/null +++ b/12/examples/lambda.txt @@ -0,0 +1,19 @@ +data List a = { Nil, Cons a (List a) } + +defn sum l = { + case l of { + Nil -> { 0 } + Cons x xs -> { x + sum xs} + } +} + +defn map f l = { + case l of { + Nil -> { Nil } + Cons x xs -> { Cons (f x) (map f xs) } + } +} + +defn main = { + sum (map \x -> { x * x } (map (\x -> { x + x }) (Cons 1 (Cons 2 (Cons 3 Nil))))) +} diff --git a/12/examples/letin.txt b/12/examples/letin.txt new file mode 100644 index 0000000..9e163b2 --- /dev/null +++ b/12/examples/letin.txt @@ -0,0 +1,47 @@ +data Bool = { True, False } + +data List a = { Nil, Cons a (List a) } + +defn if c t e = { + case c of { + True -> { t } + False -> { e } + } +} + +defn mergeUntil l r p = { + let { + defn mergeLeft nl nr = { + case nl of { + Nil -> { Nil } + Cons x xs -> { if (p x) (Cons x (mergeRight xs nr)) Nil } + } + } + defn mergeRight nl nr = { + case nr of { + Nil -> { Nil } + Cons x xs -> { if (p x) (Cons x (mergeLeft nl xs)) Nil } + } + } + } in { + mergeLeft l r + } +} + +defn const x y = { x } + +defn sum l = { + case l of { + Nil -> { 0 } + Cons x xs -> { x + sum xs } + } +} + +defn main = { + let { + defn firstList = { Cons 1 (Cons 3 (Cons 5 Nil)) } + defn secondList = { Cons 2 (Cons 4 (Cons 6 Nil)) } + } in { + sum (mergeUntil firstList secondList (const True)) + } +} diff --git a/12/examples/packed.txt b/12/examples/packed.txt new file mode 100644 index 0000000..8f25f71 --- /dev/null +++ b/12/examples/packed.txt @@ -0,0 +1,23 @@ +data Pair a b = { Pair a b } + +defn packer = { + let { + data Packed a = { Packed a } + defn pack a = { Packed a } + defn unpack p = { + case p of { + Packed a -> { a } + } + } + } in { + Pair pack unpack + } +} + +defn main = { + case packer of { + Pair pack unpack -> { + unpack (pack 3) + } + } +}