<div dir="ltr"><div>OK, so if the user enters "ABC\n" then the following happens:</div><div><br></div><div>x <- getChar --A</div><div><br></div><div> x <- getChar --B</div><div> </div><div> x <- getChar --B</div>
<div><br></div><div> x <- getChar --\n</div><div><br></div><div>levels are different contexts? levels of recursion.</div><div><br></div><div>then when recursion ends get 'A' : 'B' : 'C' : []</div>
<div><br></div><div>So does the (x:xs) syntax mean this?</div><div><br></div><div>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?</div>
<div><br></div><div>I suppose it does.</div><div><br></div><div>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)</div>
<div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On 7 January 2014 17:15, David McBride <span dir="ltr"><<a href="mailto:toad3k@gmail.com" target="_blank">toad3k@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">You have the idea. The x is fetched with getChar, then it sits in<br>
that context until the return is executed.<br>
<br>
So the x is sitting there and getLine' is called. It makes its own x<br>
via getChar, then maybe getLine' is called again. Each getLine' sits<br>
there with its own version of x until finally the last getLine' get's<br>
a \n, and then returns a []. Then the whole thing unwinds by<br>
prepending x to [], then x to [x], then another x to [x,x], until<br>
there are no more x's to return, and you have the whole string.<br>
<br>
Hopefully that paragraph makes sense.<br>
<div><div class="h5"><br>
On Tue, Jan 7, 2014 at 11:54 AM, Angus Comber <<a href="mailto:anguscomber@gmail.com">anguscomber@gmail.com</a>> wrote:<br>
> Before looking at getLine, I can understand this:<br>
><br>
> getnumber :: IO Int<br>
> getnumber = do x <- getChar<br>
> if isDigit x then<br>
> return (ord x - ord '0')<br>
> else<br>
> return 0<br>
><br>
> OK, it is not a very useful function but at least I understand it. return<br>
> is required so that the function returns an IO Int.<br>
><br>
> But I don't understand this:<br>
><br>
> getLine' :: IO String<br>
> getLine' = do x <- getChar<br>
> if x == '\n' then<br>
> return []<br>
> else<br>
> do<br>
> xs <- getLine'<br>
> return (x:xs)<br>
><br>
><br>
> I can understand what will happen if a user enters a newline (only). return<br>
> [] brings the empty list into the monadic world.<br>
><br>
> But what is happening if x is not a newline?<br>
><br>
> xs <- getLine' will recursively call getChar and retrieve another character<br>
> from the input stream. But it will do this BEFORE the return (x:xs) - so<br>
> what is happening to all the head elements in the list - the x element?<br>
><br>
> It is difficult to picture in my mind how this is working. I understand<br>
> recursion but this looks tricky.<br>
><br>
> Can someone help me work this out?<br>
><br>
><br>
</div></div>> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</blockquote></div><br></div>