[ create a new paste ] login | about

Link: http://codepad.org/lfGuuSlL    [ raw code | output | fork ]

Haskell, pasted on Oct 22:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
data Tree a = Leaf a | Fork (Tree a) a (Tree a)
  deriving Show

numberTree :: Tree () -> Tree Integer
numberTree tree = fst $ numnum tree 1
  where
    numnum :: Tree () -> Integer -> (Tree Integer, Integer)
    numnum (Leaf ())     n = (Leaf n, n+1)
    numnum (Fork l () r) n = (Fork l' ln r', rn)
      where
        (l',ln) = numnum l n
        (r',rn) = numnum r (ln+1)

main :: IO()
main = do
  print $ numberTree (Leaf ())
  print $ numberTree (Fork (Leaf ()) () (Leaf ()))
  print $ numberTree (Fork (Leaf ()) () (Fork (Fork (Leaf ()) () (Leaf ())) () (Leaf ())))


Output:
1
2
3
Leaf 1
Fork (Leaf 1) 2 (Leaf 3)
Fork (Leaf 1) 2 (Fork (Fork (Leaf 3) 4 (Leaf 5)) 6 (Leaf 7))


Create a new paste based on this one


Comments: