1 -- from the Glasgow Haskell Compiler sources (/Control/Monad/State/Strict.hs)
3 -- An example from /The Craft of Functional Programming/, Simon
4 -- Thompson (<http://www.cs.kent.ac.uk/people/staff/sjt/>),
5 -- Addison-Wesley 1999: \"Given an arbitrary tree, transform it to a
6 -- tree of integers in which the original elements are replaced by
7 -- natural numbers, starting from 0. The same element has to be
8 -- replaced by the same number at every occurrence, and when we meet
9 -- an as-yet-unvisited element we have to find a \'new\' number to match
12 -- > data Tree a = Nil | Node a (Tree a) (Tree a) deriving (Show, Eq)
13 -- > type Table a = [a]
15 -- > numberTree :: Eq a => Tree a -> State (Table a) (Tree Int)
16 -- > numberTree Nil = return Nil
17 -- > numberTree (Node x t1 t2)
18 -- > = do num <- numberNode x
19 -- > nt1 <- numberTree t1
20 -- > nt2 <- numberTree t2
21 -- > return (Node num nt1 nt2)
23 -- > numberNode :: Eq a => a -> State (Table a) Int
25 -- > = do table <- get
26 -- > (newTable, newPos) <- return (nNode x table)
29 -- > nNode:: (Eq a) => a -> Table a -> (Table a, Int)
31 -- > = case (findIndexInList (== x) table) of
32 -- > Nothing -> (table ++ [x], length table)
33 -- > Just i -> (table, i)
34 -- > findIndexInList :: (a -> Bool) -> [a] -> Maybe Int
35 -- > findIndexInList = findIndexInListHelp 0
36 -- > findIndexInListHelp _ _ [] = Nothing
37 -- > findIndexInListHelp count f (h:t)
40 -- > else findIndexInListHelp (count+1) f t
42 -- numTree applies numberTree with an initial state:
44 -- > numTree :: (Eq a) => Tree a -> Tree Int
45 -- > numTree t = evalState (numberTree t) []
47 -- > testTree = Node "Zero" (Node "One" (Node "Two" Nil Nil) (Node "One" (Node "Zero" Nil Nil) Nil)) Nil
48 -- > numTree testTree => Node 0 (Node 1 (Node 2 Nil Nil) (Node 1 (Node 0 Nil Nil) Nil)) Nil
50 -- sumTree is a little helper function that does not use the State monad:
52 -- > sumTree :: (Num a) => Tree a -> a
54 -- > sumTree (Node e t1 t2) = e + (sumTree t1) + (sumTree t2)