From dc15d5bd73107b5578b8f26c70bba1339041be0a Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Fri, 10 Mar 2023 16:48:53 +0100 Subject: 3_a_2: Add solution --- ch03/3_a_2.hs | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 ch03/3_a_2.hs (limited to 'ch03/3_a_2.hs') 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)) -- cgit v1.2.3