diff --git a/11/examples/list.txt b/11/examples/list.txt new file mode 100644 index 0000000..159bd1c --- /dev/null +++ b/11/examples/list.txt @@ -0,0 +1,32 @@ +data List a = { Nil, Cons a (List a) } + +defn map f l = { + case l of { + Nil -> { Nil } + Cons x xs -> { Cons (f x) (map f xs) } + } +} + +defn foldl f b l = { + case l of { + Nil -> { b } + Cons x xs -> { foldl f (f b x) xs } + } +} + +defn foldr f b l = { + case l of { + Nil -> { b } + Cons x xs -> { f x (foldr f b xs) } + } +} + +defn list = { Cons 1 (Cons 2 (Cons 3 (Cons 4 Nil))) } + +defn add x y = { x + y } +defn sum l = { foldr add 0 l } + +defn skipAdd x y = { y + 1 } +defn length l = { foldr skipAdd 0 l } + +defn main = { sum list + length list } diff --git a/11/examples/pair.txt b/11/examples/pair.txt new file mode 100644 index 0000000..3ed8362 --- /dev/null +++ b/11/examples/pair.txt @@ -0,0 +1,17 @@ +data Pair a b = { MkPair a b } + +defn fst p = { + case p of { + MkPair a b -> { a } + } +} + +defn snd p = { + case p of { + MkPair a b -> { b } + } +} + +defn pair = { MkPair 1 (MkPair 2 3) } + +defn main = { fst pair + snd (snd pair) }