Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://www.haskell.org/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: Picking apart getLine (Angus Comber)
2. Re: Picking apart getLine (David McBride)
3. combine expressions in do block (Renah Scarowsky)
4. Re: combine expressions in do block (Daniel Trstenjak)
----------------------------------------------------------------------
Message: 1
Date: Tue, 7 Jan 2014 17:38:19 +0000
From: Angus Comber <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Picking apart getLine
Message-ID:
<caatguhvpehsmku4kcuu1fgj-f2wnkr11jpbhvcmgffrb59z...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
OK, so if the user enters "ABC\n" then the following happens:
x <- getChar --A
x <- getChar --B
x <- getChar --B
x <- getChar --\n
levels are different contexts? levels of recursion.
then when recursion ends get 'A' : 'B' : 'C' : []
So does the (x:xs) syntax mean this?
I thought of (x:xs) as 'A' : ['B','C'] But can the xs mean simply the
rest? ie (x:xs) in this case x = 'A' and xs represents the rest 'B' : 'C'
: [] Is that maybe the way to think of it?
I suppose it does.
The confusing bit is how all the x's after the first one (ie after 'A') are
represented in returm (x:xs). But I am now thinking that ['B','C'] is
actually the same as 'B' : 'C' : [] and all that the 'B' and 'C' and also
the last [] are the xs part of (x:xs)
On 7 January 2014 17:15, David McBride <[email protected]> wrote:
> You have the idea. The x is fetched with getChar, then it sits in
> that context until the return is executed.
>
> So the x is sitting there and getLine' is called. It makes its own x
> via getChar, then maybe getLine' is called again. Each getLine' sits
> there with its own version of x until finally the last getLine' get's
> a \n, and then returns a []. Then the whole thing unwinds by
> prepending x to [], then x to [x], then another x to [x,x], until
> there are no more x's to return, and you have the whole string.
>
> Hopefully that paragraph makes sense.
>
> On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber <[email protected]>
> wrote:
> > Before looking at getLine, I can understand this:
> >
> > getnumber :: IO Int
> > getnumber = do x <- getChar
> > if isDigit x then
> > return (ord x - ord '0')
> > else
> > return 0
> >
> > OK, it is not a very useful function but at least I understand it.
> return
> > is required so that the function returns an IO Int.
> >
> > But I don't understand this:
> >
> > getLine' :: IO String
> > getLine' = do x <- getChar
> > if x == '\n' then
> > return []
> > else
> > do
> > xs <- getLine'
> > return (x:xs)
> >
> >
> > I can understand what will happen if a user enters a newline (only).
> return
> > [] brings the empty list into the monadic world.
> >
> > But what is happening if x is not a newline?
> >
> > xs <- getLine' will recursively call getChar and retrieve another
> character
> > from the input stream. But it will do this BEFORE the return (x:xs) - so
> > what is happening to all the head elements in the list - the x element?
> >
> > It is difficult to picture in my mind how this is working. I understand
> > recursion but this looks tricky.
> >
> > Can someone help me work this out?
> >
> >
> > _______________________________________________
> > Beginners mailing list
> > [email protected]
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140107/76d5b7ce/attachment-0001.html>
------------------------------
Message: 2
Date: Tue, 7 Jan 2014 12:51:50 -0500
From: David McBride <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Picking apart getLine
Message-ID:
<can+tr40_wjpdj7x7tecqf9q6gs94uzi+q7ssypxdtgjh5bn...@mail.gmail.com>
Content-Type: text/plain; charset="iso-8859-1"
You have it completely correct.
On Tue, Jan 7, 2014 at 12:38 PM, Angus Comber <[email protected]> wrote:
> OK, so if the user enters "ABC\n" then the following happens:
>
> x <- getChar --A
>
> x <- getChar --B
>
> x <- getChar --B
>
> x <- getChar --\n
>
> levels are different contexts? levels of recursion.
>
> then when recursion ends get 'A' : 'B' : 'C' : []
>
> So does the (x:xs) syntax mean this?
>
> I thought of (x:xs) as 'A' : ['B','C'] But can the xs mean simply the
> rest? ie (x:xs) in this case x = 'A' and xs represents the rest 'B' : 'C'
> : [] Is that maybe the way to think of it?
>
> I suppose it does.
>
> The confusing bit is how all the x's after the first one (ie after 'A')
> are represented in returm (x:xs). But I am now thinking that ['B','C'] is
> actually the same as 'B' : 'C' : [] and all that the 'B' and 'C' and also
> the last [] are the xs part of (x:xs)
>
>
>
> On 7 January 2014 17:15, David McBride <[email protected]> wrote:
>
>> You have the idea. The x is fetched with getChar, then it sits in
>> that context until the return is executed.
>>
>> So the x is sitting there and getLine' is called. It makes its own x
>> via getChar, then maybe getLine' is called again. Each getLine' sits
>> there with its own version of x until finally the last getLine' get's
>> a \n, and then returns a []. Then the whole thing unwinds by
>> prepending x to [], then x to [x], then another x to [x,x], until
>> there are no more x's to return, and you have the whole string.
>>
>> Hopefully that paragraph makes sense.
>>
>> On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber <[email protected]>
>> wrote:
>> > Before looking at getLine, I can understand this:
>> >
>> > getnumber :: IO Int
>> > getnumber = do x <- getChar
>> > if isDigit x then
>> > return (ord x - ord '0')
>> > else
>> > return 0
>> >
>> > OK, it is not a very useful function but at least I understand it.
>> return
>> > is required so that the function returns an IO Int.
>> >
>> > But I don't understand this:
>> >
>> > getLine' :: IO String
>> > getLine' = do x <- getChar
>> > if x == '\n' then
>> > return []
>> > else
>> > do
>> > xs <- getLine'
>> > return (x:xs)
>> >
>> >
>> > I can understand what will happen if a user enters a newline (only).
>> return
>> > [] brings the empty list into the monadic world.
>> >
>> > But what is happening if x is not a newline?
>> >
>> > xs <- getLine' will recursively call getChar and retrieve another
>> character
>> > from the input stream. But it will do this BEFORE the return (x:xs) -
>> so
>> > what is happening to all the head elements in the list - the x element?
>> >
>> > It is difficult to picture in my mind how this is working. I understand
>> > recursion but this looks tricky.
>> >
>> > Can someone help me work this out?
>> >
>> >
>> > _______________________________________________
>> > Beginners mailing list
>> > [email protected]
>> > http://www.haskell.org/mailman/listinfo/beginners
>> >
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140107/613c4e60/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 8 Jan 2014 10:09:18 +0200
From: "Renah Scarowsky" <[email protected]>
To: <[email protected]>
Subject: [Haskell-beginners] combine expressions in do block
Message-ID: <022e01cf0c48$eee09cf0$cca1d6d0$@com>
Content-Type: text/plain; charset="us-ascii"
Is it possible to bind two expressions together within a case inside a do
block?
Given the following code running in the writer monad:
f a = do
case g a of
Nothing -> return ()
Just b ->
h a (field1 b) (field2 b)
case g a of
Nothing -> return ()
Just b ->
i a (j a) (field1 b) (field2 b)
How can I combine these two cases together (or otherwise simplify the code)?
Thanks,
Renah Scarowsky
Suite Solutions
Create>Manage>Deploy
<http://www.suite-sol.com/> http://www.suite-sol.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20140108/e6605524/attachment-0001.html>
------------------------------
Message: 4
Date: Wed, 8 Jan 2014 09:21:59 +0100
From: Daniel Trstenjak <[email protected]>
To: [email protected]
Subject: Re: [Haskell-beginners] combine expressions in do block
Message-ID: <20140108082159.GA2379@machine>
Content-Type: text/plain; charset=us-ascii
Hi Renah,
On Wed, Jan 08, 2014 at 10:09:18AM +0200, Renah Scarowsky wrote:
> How can I combine these two cases together (or otherwise simplify the code)?
I don't know if I'm understanding you correctly, but something like this?
f a = do
case g a of
Nothing -> return ()
Just b -> do
h a (field1 b) (field2 b)
i a (j a) (field1 b) (field2 b)
Greetings,
Daniel
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 67, Issue 13
*****************************************