Difference between revisions of "FP Laboratory 11"
Jump to navigation
Jump to search
Line 2: | Line 2: | ||
* Create a data type that defines binary tree where values are stored in leaves and also in branches. | * Create a data type that defines binary tree where values are stored in leaves and also in branches. | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | data Tree a = Leaf a | ||
+ | | Branch a (Tree a) (Tree a) deriving (Show) | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* Prepare an example of a binary tree. | * Prepare an example of a binary tree. | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | testTree1 :: Tree Int | ||
+ | testTree1 = Branch 12 (Branch 23 (Leaf 34) (Leaf 45)) (Leaf 55) | ||
+ | |||
+ | testTree2 :: Tree Char | ||
+ | testTree2 = Branch 'a' (Branch 'b' (Leaf 'c') (Leaf 'd')) (Leaf 'e') | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* Create a function that counts the values stored in the leaves. | * Create a function that counts the values stored in the leaves. | ||
− | * Create a function that extracts all values from the leaves into | + | |
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | sum' :: Tree Int -> Int | ||
+ | sum' (Leaf x) = x | ||
+ | sum' (Branch x l r) = sum' l + x + sum' r | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
+ | * Create a function that extracts all values from the leaves into an list. | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | toList :: Tree a -> [a] | ||
+ | toList (Leaf x) = [x] | ||
+ | toList (Branch x l r) = toList l ++ [x] ++ toList r | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> | ||
+ | |||
* One possibility how to represent a tree in a textual form is <code>a(b(d,e),c(e,f(g,h)))</code>. Create functions that are able to read and store a tree in such a notation. | * One possibility how to represent a tree in a textual form is <code>a(b(d,e),c(e,f(g,h)))</code>. Create functions that are able to read and store a tree in such a notation. | ||
+ | |||
+ | <div class="mw-collapsible mw-collapsed" data-collapsetext="Hide solution" data-expandtext="Show solution"> | ||
+ | <syntaxhighlight lang="Haskell"> | ||
+ | toString :: Show a => Tree a -> String | ||
+ | toString (Leaf x) = show x | ||
+ | toString (Branch x l r) = show x ++ "(" ++ (toString l) ++ "," ++ (toString r) ++ ")" | ||
+ | |||
+ | fromString :: Read a => String -> Tree a | ||
+ | fromString inp = fst (fromString' inp) where | ||
+ | fromString' :: Read a => String -> (Tree a,String) | ||
+ | fromString' inp = | ||
+ | let | ||
+ | before = takeWhile (\x -> x /='(' && x /=',' && x/=')') inp | ||
+ | after = dropWhile (\x -> x /='(' && x /=',' && x/=')') inp | ||
+ | value = read before | ||
+ | in if null after || head after /= '(' then (Leaf value, after) else | ||
+ | let | ||
+ | (l,after') = fromString' (tail after) | ||
+ | (r,after'') = fromString' (tail after') | ||
+ | in (Branch value l r, tail after'') | ||
+ | </syntaxhighlight> | ||
+ | </div> | ||
+ | <div style="clear:both"></div> |
Revision as of 09:58, 24 September 2020
Binary Trees
- Create a data type that defines binary tree where values are stored in leaves and also in branches.
data Tree a = Leaf a
| Branch a (Tree a) (Tree a) deriving (Show)
- Prepare an example of a binary tree.
testTree1 :: Tree Int
testTree1 = Branch 12 (Branch 23 (Leaf 34) (Leaf 45)) (Leaf 55)
testTree2 :: Tree Char
testTree2 = Branch 'a' (Branch 'b' (Leaf 'c') (Leaf 'd')) (Leaf 'e')
- Create a function that counts the values stored in the leaves.
sum' :: Tree Int -> Int
sum' (Leaf x) = x
sum' (Branch x l r) = sum' l + x + sum' r
- Create a function that extracts all values from the leaves into an list.
toList :: Tree a -> [a]
toList (Leaf x) = [x]
toList (Branch x l r) = toList l ++ [x] ++ toList r
- One possibility how to represent a tree in a textual form is
a(b(d,e),c(e,f(g,h)))
. Create functions that are able to read and store a tree in such a notation.
toString :: Show a => Tree a -> String
toString (Leaf x) = show x
toString (Branch x l r) = show x ++ "(" ++ (toString l) ++ "," ++ (toString r) ++ ")"
fromString :: Read a => String -> Tree a
fromString inp = fst (fromString' inp) where
fromString' :: Read a => String -> (Tree a,String)
fromString' inp =
let
before = takeWhile (\x -> x /='(' && x /=',' && x/=')') inp
after = dropWhile (\x -> x /='(' && x /=',' && x/=')') inp
value = read before
in if null after || head after /= '(' then (Leaf value, after) else
let
(l,after') = fromString' (tail after)
(r,after'') = fromString' (tail after')
in (Branch value l r, tail after'')