Trying to iterate over a forest Haskell compilation error

155 Views Asked by At

I try to assign to each node of a forest a number, such that there will be no 2 nodes with the same number. I try to use 2 functions which call eachother recursive but I get some compilation errors. Here is the code :

numberTree :: Int -> Tree a -> (Tree Int,Int)

numberTree n (Node c []) = (Node n [], n+1)
numberTree n (Node _ (x:xs)) =  (Node n  fst.numberForest  (n+1) xs, snd.numberForest   (n+1) xs)


numberForest :: Int -> [Tree a] -> ([Tree Int],Int)

numberForest n (x:xs) = ((fst(numberTree n x )):(fst(numberForest (n+1) xs)), snd(numberForest (n+1) xs)+1)

numberForest n (x:xs) = ((fst(numberTree n x )):(fst(numberForest (n+1) xs)), snd(numberForest (n+1) xs)+1)

The errors I get are :

.hs:27:34: Couldn't match expected type b0 -> c0' with actual typeTree Int Possible cause: Node' is applied to too many arguments In the first argument of(.)', namely `Node n fst' In the expression: Node n fst . numberForest (n + 1) xs

.hs:27:34:
Couldn't match expected type `Tree Int' with actual type `a0 -> c0
In the expression: Node n fst . numberForest (n + 1) xs
In the expression:
  (Node n fst . numberForest (n + 1) xs,
   snd . numberForest (n + 1) xs)
In an equation for `numberTree':
    numberTree n (Node _ (x : xs))
      = (Node n fst . numberForest (n + 1) xs,
         snd . numberForest (n + 1) xs)

 .hs:27:42:
Couldn't match type `(a1, b1) -> a1' with `[Tree Int]'
Expected type: Forest Int
  Actual type: (a1, b1) -> a1
Probable cause: `fst' is applied to too few arguments
In the second argument of `Node', namely `fst'
In the first argument of `(.)', namely `Node n fst'

 .hs:27:46:
Couldn't match expected type `a0 -> b0'
            with actual type `([Tree Int], Int)'
Possible cause: `numberForest' is applied to too many arguments
In the second argument of `(.)', namely `numberForest (n + 1) xs'
In the expression: Node n fst . numberForest (n + 1) xs

.hs:27:70:
Couldn't match expected type `Int' with actual type `a2 -> c1'
In the expression: snd . numberForest (n + 1) xs
In the expression:
  (Node n fst . numberForest (n + 1) xs,
   snd . numberForest (n + 1) xs)
In an equation for `numberTree':
    numberTree n (Node _ (x : xs))
      = (Node n fst . numberForest (n + 1) xs,
         snd . numberForest (n + 1) xs)

.hs:27:74:
Couldn't match expected type `a2 -> (a3, c1)'
            with actual type `([Tree Int], Int)'
Possible cause: `numberForest' is applied to too many arguments
In the second argument of `(.)', namely `numberForest (n + 1) xs'
In the expression: snd . numberForest (n + 1) xs

What is wrong and how should I solve this ?

2

There are 2 best solutions below

0
On

This line

numberTree n (Node _ (x:xs)) =  (Node n  fst.numberForest  (n+1) xs, snd.numberForest   (n+1) xs)

actually means

numberTree n (Node _ (x:xs)) =  ( (Node n fst) . (numberForest (n+1) xs)
                                , snd . (numberForest (n+1) xs))

which tries to compose trees instead of functions, causing the compiler to complain. You probably want something like this:

numberTree n (Node _ (x:xs)) =  ( Node n  (fst (numberForest  (n+1) xs))
                                , snd (numberForest (n+1) xs))

However, be careful that the code above is computing numberForest (n+1) xs twice, causing an exponential blowup. You can avoid that e.g. using a let ... in ... as follows

numberTree n (Node _ (x:xs)) =  let result = numberForest (n+1) xs
                                in (Node n (fst result), snd result)

You can further improve on this using pattern matching inside the let:

numberTree n (Node _ (x:xs)) =  let (forest, n') = numberForest (n+1) xs
                                in (Node n forest, n')
0
On

The state monad is your friend.

import Control.Monad.Trans.State

data Tree   a = Node a (Forest a)
type Forest a = [Tree a]

numberTreeS :: Tree a -> State Int (Tree Int)
numberTreeS (Node _ xs) = do
    n <- get
    put (n + 1)
    xs' <- numberForestS xs
    return $ Node n xs'

numberForestS :: Forest a -> State Int (Forest Int)
numberForestS = mapM numberTreeS

numberForest :: Forest a -> Forest Int
numberForest xs = evalState (numberForestS xs) 0

This is more readable, than explicit state passing.