I’m on the Making Our Own Types and Typeclasses chapter.

With prompting from the tutorial, knocked up the classic binary tree :

data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq) root x = Node x EmptyTree EmptyTree tInsert x EmptyTree = root x tInsert x (Node val left right) | x < val = Node val (tInsert x left) right | otherwise = Node val left (tInsert x right) tFind x EmptyTree = False tFind x (Node val left right) | x == val = True | x < val = tFind x left | x > val = tFind x right

Which isn’t really all that impressive.

But what I think I’m more chuffed about is what I did next. My first attempt at a quick and dirty function to fill a tree from a list of numbers :

listToTree [] = EmptyTree listToTree (x:xs) = foldl insert (root x) xs where insert = (tree val -> tInsert val tree)

That looks like it’s quite elegant Haskell, no?

Update : Or maybe not. Because I scroll down and see the next thing the tutorial does …

let numsTree = foldr tInsert EmptyTree [5, 19, 2, 52, 43, 32, 99]

So I basically missed the fact that the original tInsert was already sufficient to pass to the fold (at least in its foldr form, won’t work with foldl because the arguments are the wrong way around.)