[Haskell-beginners] enumaratioin question

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


When you use record syntax, accessors are automatically created for you. So,

data Person = Person {
      name     :: String
    , age      :: Integer
    , favThing :: String
    }

means that name, age and favThing are functions that do exactly what you
want:

    name     :: Person -> String
    age      :: Person -> Integer
    favThing :: Person -> String

So you just need to call age on a Person value to get the age.
Due to this functionality, the names in record syntax can not start with an
uppercase letter.

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

>  Thanks,
>
> That is not what I mean ,
>
> I mean this :
>
> data Person = Person
>       { name :: String ,
>         Age :: Integer ,
>         FavThing :: String  }
>
>
> and i want to get the Age I could do this :
>
> getAge (Person {age = ag}) = ag
>
> Roelof
>
>
> Sumit Sahrawat, Maths & Computing, IIT (BHU) schreef op 19-2-2015 om 17:37:
>
>  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
>
>
> _______________________________________________
> Beginners mailing listBeginners at haskell.orghttp://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/4b5c43a7/attachment.html>


More information about the Beginners mailing list