S04

Synopsis

# Exercise 2

Using the definitions

  foldr f b []     = b
foldr f b (x:xs) = x f foldr f b xs


and

  foldl f b []     = b
foldl f b (x:xs) = foldl f (b f x) xs


The expression foldr (-) 0 [1,2,3] can be evaluated as

  foldr (-) 0 [1,2,3]
= 1 - foldr (-) 0 [2,3]
= 1 - (2 - foldr (-) 0 [3])
= 1 - (2 - (3 - foldr (-) 0 []))
= 1 - (2 - (3 - 0))
= 2


and the expression foldl (-) 0 [1,2,3] as

  foldl (-) 0 [1,2,3]
= foldl (-) (0 - 1) [2,3]
= foldl (-) ((0 - 1) - 2) [3]
= foldl (-) (((0 - 1) - 2) - 3) []
= ((0 - 1) - 2) - 3
= - 6


# Exercise 3

isSorted :: Ord a => BTree a -> Bool Source #

See Grep.

# Exercise 6

showBTree :: Show a => BTree a -> String Source #