[Haskell-beginners] Re: N-ary tree search problems

Christian Maeder Christian.Maeder at dfki.de
Fri Nov 6 05:00:51 EST 2009

Ryan Temple schrieb:
> I'm making a general purpose N-ary tree and im coming up with
> "unexpected '=' on line 17" as an error.  I have spent a fair while
> trying to work out why this isn't accepting the case that an Empty gtree
> returns false for any member search.  i realise this is probably a very
> trivial error but any help would be appreciated:
> module GTrees where
> data Gtree = Empty |
>              Leaf String |
>              Node String [Gtree]
>              deriving (Show)

You may want to consider a node with a singleton list as "Leaf" and a
node with an empty list as "Empty" (if the additional "String" for
"Node" does not harm for an empty tree).

> --Tests if a given string is a member of the tree
> gtreeMember :: (Ord a) => a -> Gtree a -> Bool
>   gtreeMember y Empty = False  -- line 17
>   gtreeMember y (Leaf x) = (x==y)
>   gtreeMember y (Node x tree)
>       |x==y = True
>       |otherwise gtreeMember tree

After proper indentation and deciding if Gtree is polymorphic or not in
the signature and data type, this last line will not work:
1. there must be a "=" following "otherwise"
2. gtreeMember expects two arguments
3. the variable "tree" is a list of Gtrees
   whereas gtreeMember only works for a single Gtree.

HTH Christian

More information about the Beginners mailing list