aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-03-10 16:48:53 +0100
committerJan Sucan <jan@jansucan.com>2023-03-10 16:48:53 +0100
commitdc15d5bd73107b5578b8f26c70bba1339041be0a (patch)
tree9f48d07105662ed139f20bb1fadf0005c02cc4f3
parent5cd664fe805f5c026c908ac0202edc30f28106b7 (diff)
3_a_2: Add solution
-rw-r--r--README.md4
-rw-r--r--ch03/3_a_2.hs25
2 files changed, 27 insertions, 2 deletions
diff --git a/README.md b/README.md
index ae090f7..22b09be 100644
--- a/README.md
+++ b/README.md
@@ -1,5 +1,5 @@
# Real World Haskell solutions
-
+0;115;0c
These are solutions to exercises from me reading the first edition of the [the
Real World Haskell book](https://book.realworldhaskell.org/). I hope they will
be helpful in your journey to the world of Haskell.
@@ -52,7 +52,7 @@ more visible in the list the first exercise of a group is in bold italics.
| 2_b_2 | yes | | |
| 2_b_3 | yes | | |
| **_3_a_1_** | yes | 60 | 3. Defining types, streamlining functions |
-| 3_a_2 | | | |
+| 3_a_2 | yes | | |
| **_3_b_1_** | | 69 | |
| 3_b_2 | | | |
| 3_b_3 | | | |
diff --git a/ch03/3_a_2.hs b/ch03/3_a_2.hs
new file mode 100644
index 0000000..42282e7
--- /dev/null
+++ b/ch03/3_a_2.hs
@@ -0,0 +1,25 @@
+-- Define a tree type that has only one constructor, like our Java
+-- example. Instead of the Empty constructor, use the Maybe type to refer to a
+-- node's children.
+
+-- The assignment doesn't explicitly specify whether it should be possible to
+-- create an empty tree at the root level, so this tree type doesn't support
+-- that.
+
+data Tree a = Node a (Maybe (Tree a)) (Maybe (Tree a))
+ deriving (Show)
+
+
+-- parent
+-- /\
+-- / \
+-- left child right child
+-- /
+-- /
+-- left child
+
+simpleTree = Node "parent" (Just (Node "left child" Nothing
+ Nothing))
+ (Just (Node "right child" (Just (Node "left child" Nothing
+ Nothing))
+ Nothing))