Find an element in a BST Haskell -
i rewriting search-for-an-element function in bst on own, encountered error i'm not of means (error occured on line of x (empty) = false)
occurs check: cannot construct infinite type: = tree when generalising type(s) `searchtree' here i've come with:
data tree = empty | node (tree a) (tree a) deriving (show, read, eq) searchtree :: (ord a) => -> tree -> bool searchtree x (empty) = false searchtree x (node left y right) |y == x = true |y > x = searchtree x left |y < x = searchtree x right
you have node (tree a) (tree a), pattern-match (node left y right). infers following types: left :: a; y, right :: tree a. compare y :: tree a x :: a - forces them have same type, possible if tree a same type a. that's impossible1, infinite type, ghc complains.
the solution fix pattern match: should (node y left right), match data constructor.
1 it's theoretically possible type, not useful one. a ~ tree a can hold, if a ~ tree (tree (tree (tree (tree ...)))).
Comments
Post a Comment