[Haskell-cafe] Convert Either to Tree - Occurs check

André Batista Martins andre_bm at netcabo.pt
Thu Oct 21 16:11:00 EDT 2010


hello,
 i want convert  "Either" to a tree.
 Example:
                  Either ( Either  1 2 ) ( Either 3 4)     ---- >
Branch ( Branch (Leafl 1)  (Leafr2) ) ( Branch (Leafl 3)  (Leafr4)) )



Code:
data TreeE a b  = Empty |Leafl a | Leafr b | Branch (TreeE a b ) (TreeE a b)
deriving Show

f3 (Right b) (Branch l r) =case( isRight(b) || isLeft(b) )of
                true -> Branch l (f3 b r)
                false -> Branch l (Leafl b)
f3 (Left b) (Branch l r) = case( isRight(b) || isLeft(b) )of
                true -> Branch (f3 b l) r
                false -> Branch (Leafl b) r

Error:
 Occurs check: cannot construct the infinite type: b = Either a b
      Expected type: Either a b -> TreeE t t1 -> TreeE t t1
      Inferred type: Either a (Either a b) -> TreeE t t1 -> t2
    In the second argument of `Branch', namely `(f3 b r)'
    In the expression: Branch l (f3 b r)

I don't understand why this happen...
Can anyone help me?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20101021/4595a5c7/attachment.html


More information about the Haskell-Cafe mailing list