
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

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] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

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 ; beginners@haskell.org ; haskell-cafe@haskell.org 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] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH

On Thu, Jan 1, 2009 at 12:36 AM, Max.cs
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

Hello Max.cs, Thursday, January 1, 2009, 11:36:24 AM, you wrote: seems that you come from dynamic languages :) Haskell has static typing meaning that your function can accept either Tree or a as arguments. so you should convert a to Tree explicitly, using Leaf
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 ; beginners@haskell.org ; haskell-cafe@haskell.org
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
-- Best regards, Bulat mailto:Bulat.Ziganshin@gmail.com

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

You need some type constructor: data Tree a = Leaf a | Branch (Tree a) (Tree a) Am 01.01.2009 um 08:32 schrieb Max.cs:
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 _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (6)
-
Adrian Neumann
-
Alexander Dunlap
-
Brandon S. Allbery KF8NH
-
Bulat Ziganshin
-
Max.cs
-
Thomas Davie