[Haskell-beginners] New to Haskell: constructing Data Types
Antoine Latter
aslatter at gmail.com
Sat Feb 5 05:46:49 CET 2011
On Fri, Feb 4, 2011 at 10:33 PM, Tyler Hayes <tgh at pdx.edu> wrote:
> As an exercise, I'm attempting to create an abstract syntax tree for a
> very small language:
>
> data Expression = Variable String
> | Integer
> | Expression BinOp Expression
> deriving Show
>
> data Statement = Assignment Variable Expression --error!
> | If Expression Statement Statement
> | While Expression Statement
> | Compound Statement
> deriving Show
>
> data BinOp = Add
> | Mult
> | LessThan
> | GreaterThan
> deriving Show
>
> However, there is a problem with having "Variable" as an argument to the
> "Assignment" constructor of the "Statement" data type:
>
In your example, you've declared three types: Expression, Statement
and BinOp. So when you ask for a type named 'Variable' it can't find
one. Perhaps you meant 'String' ?
In your above example, you do have a data constrictor 'Variable',
which can be used as a value to construct values of type 'Expression'
(or in a pattern match over values of type Expression).
The difference between types and data constructors can be tricky at
first, especially since they appear with each other in code and have
similar naming conventions.
If I'm speaking a foreign language I can probably dig up a tutorial to
point you to :-)
Antoine
More information about the Beginners
mailing list