Difference between revisions of "FP Laboratory 10"

From Marek Běhálek Wiki
Jump to navigation Jump to search
Line 1: Line 1:
 
<translate>
 
<translate>
== Abstract data types == <!--T:1-->
+
== Binary Trees ==
 +
* Create a data type <code>Tree</code> that defines binary tree where values are stored in leaves and also in branches.
 +
</translate>
 +
 
 +
<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>
 +
 
 +
<translate>
 +
* Prepare an example of a binary tree.
 +
</translate>
 +
 
 +
<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>
 +
 
 +
<translate>
 +
* Create a function that sums all values stored in the tree.
 +
</translate>
 +
 
 +
<syntaxhighlight lang="Haskell">
 +
sum' :: Tree Int -> Int
 +
</syntaxhighlight>
 +
 
 +
<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>
 +
 
 +
<translate>
 +
* Create a function that extracts all values from the tree into an list.
 +
</translate>
 +
 
 +
<syntaxhighlight lang="Haskell">
 +
toList :: Tree a -> [a]
 +
</syntaxhighlight>
 +
 
 +
<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>
 +
 
 +
<translate>
 +
* 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.
 +
</translate>
 +
 
 +
<div style="float: right"> [[File:Video logo.png|80px|link=https://youtu.be/b0EF9WQw-uw]]</div>
 +
<syntaxhighlight lang="Haskell">
 +
toString :: Show a => Tree a -> String
 +
fromString :: Read a => String -> Tree a
 +
</syntaxhighlight>
 +
 
 +
<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>
 +
 
 +
 
 +
 
 +
<translate>
 +
== Additional exercises ==
 +
=== Abstract data types ===
 
</translate>
 
</translate>
  
Line 6: Line 104:
  
 
<translate>
 
<translate>
<!--T:2-->
 
 
* Create an implementation of the abstract data type [https://en.wikipedia.org/wiki/Stack_(abstract_data_type) <code>Stack</code>] with following functions:
 
* Create an implementation of the abstract data type [https://en.wikipedia.org/wiki/Stack_(abstract_data_type) <code>Stack</code>] with following functions:
 
</translate>
 
</translate>
Line 42: Line 139:
  
 
<translate>
 
<translate>
<!--T:3-->
 
 
* Create an implementation of the abstract data type [https://en.wikipedia.org/wiki/Queue_(abstract_data_type) <code>Queue</code>] with following functions:
 
* Create an implementation of the abstract data type [https://en.wikipedia.org/wiki/Queue_(abstract_data_type) <code>Queue</code>] with following functions:
 
</translate>
 
</translate>

Revision as of 09:31, 15 November 2023

Binary Trees

  • Create a data type Tree 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 sums all values stored in the tree.
sum' :: Tree Int -> Int
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 tree into an list.
toList :: Tree a -> [a]
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.
Video logo.png
toString :: Show a => Tree a -> String
fromString :: Read a => String -> Tree a
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'')


Additional exercises

Abstract data types

Video logo.png
  • Create an implementation of the abstract data type Stack with following functions:
push :: a -> Stack a -> Stack a
pop :: Stack a -> Stack a
top :: Stack a -> a
isEmpty :: Stack a ->Bool
module Stack(Stack, emptyS, push, pop, top, isEmpty) where
  data Stack a = Stack [a] deriving Show

  emptyS :: Stack a
  emptyS = Stack []

  push :: a -> Stack a -> Stack a
  push x (Stack y) = Stack (x:y)
  
  pop :: Stack a -> Stack a
  pop (Stack (_:xs)) = Stack xs

  top :: Stack a -> a
  top (Stack (x:_)) = x

  isEmpty :: Stack a ->Bool
  isEmpty (Stack []) = True
  isEmpty _ = False
  • Create an implementation of the abstract data type Queue with following functions:
isEmpty :: Queue a -> Bool
addQ :: a -> Queue a -> Queue a
remQ :: Queue a -> (a, Queue a)
module Queue(Queue, emptyQ, isEmptyQ, addQ, remQ) where
    data Queue a = Qu [a] deriving Show

    emptyQ :: Queue a
    emptyQ = Qu []
    
    isEmptyQ :: Queue a -> Bool
    isEmptyQ (Qu q) = null q
    
    addQ :: a -> Queue a -> Queue a
    addQ x (Qu xs) = Qu (xs++[x])
    
    remQ :: Queue a -> (a,Queue a)
    remQ q@(Qu xs) | not (isEmptyQ q) = (head xs, Qu (tail xs))
                   | otherwise        = error "remQ"