[Haskell-cafe] Re: [Haskell-beginners] pattern matching on date type
Thomas Davie
tom.davie at gmail.com
Thu Jan 1 05:04:23 EST 2009
On 1 Jan 2009, at 09:36, Max.cs wrote:
> thanks!
>
> suppose we have
>
> > data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show
>
> and how I could define a function foo :: a -> Tree a that
>
> foo a = Leaf a where a is not a type of Tree
> foo b = b where b is one of the type of Tree (Leaf
> or Branch) ?
>
> The following code seems not working......
>
> foo (Leaf a) = a
> foo a = Leaf a
>
> saying 'Couldn't match expected type `a' against inferred type
> `Btree a'
Hi again Max,
I'm assuming this is continuing from the concatT example, and that
you're struggling with first function you must pass to foldTree.
Remember the type of the function – it's not a -> Tree a, but Tree a -
> Tree a, because your leaves in the parent tree all contain trees to
glue on at that point.
So, the function you want, is the function which looks at the
parameter it's given, goes "oh, that's interesting", does nothing to
it, and hands it back to replace the Leaf. I recommend searching
hoogle for functions of type a -> a, the function you're looking for
is built in.
Bob
More information about the Haskell-Cafe
mailing list