Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
[email protected]
You can reach the person managing the list at
[email protected]
When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."
Today's Topics:
1. Re: enumaratioin question
(Sumit Sahrawat, Maths & Computing, IIT (BHU))
2. function to read file and return list of contents not
compiling (Geoffrey Bays)
3. Re: enumaratioin question (Roelof Wobben)
----------------------------------------------------------------------
Message: 1
Date: Thu, 19 Feb 2015 22:33:50 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: Roelof Wobben <[email protected]>
Cc: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] enumaratioin question
Message-ID:
<cajbew8mu+66ec2h8chbzer1atx0r4tl0aoasjmbwmvjjhjm...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
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 <[email protected]> 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 <[email protected]> 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
>> [email protected]
>> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>>
>>
>
>
> --
> Regards
>
> Sumit Sahrawat
>
>
> _______________________________________________
> Beginners mailing
> [email protected]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/4b5c43a7/attachment-0001.html>
------------------------------
Message: 2
Date: Thu, 19 Feb 2015 12:17:38 -0500
From: Geoffrey Bays <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] function to read file and return list of
contents not compiling
Message-ID:
<CAD8ukx4Axg6BPECqBorqFp4J3taXdqhB5hC9xzvXtRdZ-tp=o...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Haskellers:
I want to write a function that takes a class name and reads from a file
and then returns the
list of String of the file lines. I have experimented with various return
types, but GHC does not like any of them. Here is my function:
readData :: String -> IO String -- what type here??
readData classNameP = do
let fileName = classNameP ++ ".txt"
contents <- readFile fileName
lines contents
Not to complain, but this would not be difficult in any other language I
have tried, so I could use
some explanation. I suspect it has to do with understanding the IO Monad.
Many Thanks
Geoffrey
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150219/c8a20840/attachment-0001.html>
------------------------------
Message: 3
Date: Thu, 19 Feb 2015 18:20:51 +0100
From: Roelof Wobben <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] enumaratioin question
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150219/0ed66987/attachment.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 80, Issue 53
*****************************************