[Haskell-beginners] Picking apart getLine
Angus Comber
anguscomber at gmail.com
Tue Jan 7 17:38:19 UTC 2014
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 <toad3k at gmail.com> 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 <anguscomber at gmail.com>
> 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
> > Beginners at haskell.org
> > http://www.haskell.org/mailman/listinfo/beginners
> >
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> 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.html>
More information about the Beginners
mailing list