<div dir="ltr"><div>Before looking at getLine, I can understand this:</div><div><br></div><div>getnumber :: IO Int</div><div>getnumber = do x <- getChar</div><div>               if isDigit x then</div><div>                     return (ord x - ord '0')</div>
<div>               else</div><div>                     return 0</div><div><br></div><div>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.</div>
<div><br></div><div>But I don't understand this:</div><div><br></div><div>getLine' :: IO String</div><div>getLine'        = do x <- getChar</div><div>                     if x == '\n' then</div><div>
                       return []</div><div>                     else</div><div>                       do </div><div>                         xs <- getLine'</div><div>                         return (x:xs)</div><div>
<br></div><div><br></div><div>I can understand what will happen if a user enters a newline (only).  return [] brings the empty list into the monadic world.</div><div><br></div><div>But what is happening if x is not a newline?  </div>
<div><br></div><div>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?</div>
<div><br></div><div>It is difficult to picture in my mind how this is working.  I understand recursion but this looks tricky.</div><div><br></div><div>Can someone help me work this out?</div><div><br></div></div>