hi all, I want to define a data type Tree a that can either be a or Branch (Tree a) (Tree a)?
I tried data Tree a = a | Branch (Tree a) (Tree a) deriving Show but it seems not accpetable in haskell ? any way I could achieve this ? Thanks max -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090101/11fad6a9/attachment.htm |
On 2009 Jan 1, at 2:32, Max.cs wrote:
> data Tree a = a | Branch (Tree a) (Tree a) deriving Show > > but it seems not accpetable in haskell ? You need a constructor in both legs of the type: > data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email] system administrator [openafs,heimdal,too many hats] [hidden email] electrical and computer engineering, carnegie mellon university KF8NH -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090101/8ddd680d/attachment-0001.htm |
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'' any idea? Thanks, Max From: Brandon S. Allbery KF8NH Sent: Thursday, January 01, 2009 7:35 AM To: Max.cs Cc: Brandon S. Allbery KF8NH ; [hidden email] ; [hidden email] Subject: Re: [Haskell-cafe] definition of data On 2009 Jan 1, at 2:32, Max.cs wrote: data Tree a = a | Branch (Tree a) (Tree a) deriving Show but it seems not accpetable in haskell ? You need a constructor in both legs of the type: > data Tree a = Leaf a | Branch (Tree a) (Tree a) deriving Show -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [hidden email] system administrator [openafs,heimdal,too many hats] [hidden email] electrical and computer engineering, carnegie mellon university KF8NH -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20090101/f7240635/attachment.htm |
On Thu, Jan 1, 2009 at 12:36 AM, Max.cs <[hidden email]> 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'' > > any idea? > > Thanks, > Max You can't define such a function. foo :: a -> Tree a, but the definition foo b = b has the type a -> a, which is why the compiler says it can't match type "a" against "Tree a". In general, Haskell functions can't "look" at the type of their arguments: they are either monomorphic or are polymorphic, in which case you can only use polymorphic functions that match the polymorphic type. For the problem you are trying to solve, you probably need to encode this logic higher up in the overall function (e.g. have one function to deal with "a"s and another to deal with "Tree a"s). Hope that helps, Alex |
In reply to this post by Max cs
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' 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 |
Free forum by Nabble | Edit this page |