[Haskell-beginners] enumaratioin question

Sumit Sahrawat, Maths & Computing, IIT (BHU) sumit.sahrawat.apm13 at iitbhu.ac.in
Thu Feb 19 16:37:58 UTC 2015


I can't understand what you mean by those colons in the second definition
of Person. If you're thinking of type signatures, then that doesn't work in
haskell.
In an ADT, you give names to possible values. So "Name String" will work
whereas "Name : String" won't work.

data Person = Name String
            | Age Integer
            | FavThing String

means that Person can be *one of* these things (which is not what you want).

What you want is possible with record syntax. He'll detail it later I think.
If you're interested in learning about it beforehand, look it up in the
haskell wikibook (another great haskell resource).

More about ADTs in general:
http://en.wikibooks.org/wiki/Haskell/Type_declarations#data_and_constructor_functions
The link to the specific section:
http://en.wikibooks.org/wiki/Haskell/More_on_datatypes#Named_Fields_.28Record_Syntax.29

On 19 February 2015 at 21:58, Roelof Wobben <r.wobben at home.nl> wrote:

>  Hello,
>
> Im reading chapter 2 of the CIS 194 course about enumaratuin.
>
> Now they give this example :
>
> -- Store a person's name, age, and favourite Thing.data Person = Person String Int Thing
>   deriving Show
> brent :: Person
> brent = Person "Brent" 31 SealingWax
> stan :: Person
> stan  = Person "Stan" 94 Cabbage
> getAge :: Person -> Int
> getAge (Person _ a _) = a
>
> I understand how this works.
>
> But I wonder if there is no "better" way to get the Age.
>
> Is it now wise to make  a person data like this :
>
> data Person = Name : String
>     | Age : Integer
>     | FavThing : String
>
> And if so , how can I get the age then ?
>
> Roelof
>
>
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>


-- 
Regards

Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20150219/1d13ccc5/attachment.html>


More information about the Beginners mailing list