From 9fb9eec39445d485cb59236d2644a3d71cc84659 Mon Sep 17 00:00:00 2001 From: Danila Fedorin Date: Tue, 14 Apr 2020 16:19:54 -0700 Subject: [PATCH] Finish draft of part 11 of compiler series --- 11/examples/list.txt | 32 ++++++++++++++++++++++++++++++++ 11/examples/pair.txt | 17 +++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 11/examples/list.txt create mode 100644 11/examples/pair.txt 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) }