Exercise 3.1

myPutStr :: String -> IO ()
myPutStr [] = return ()
myPutStr (c : cs) = do putChar c
                       myPutStr cs
 
myGetLine :: IO String
myGetLine = do c <- getChar
               if c == '\n' then return "" else do l <- myGetLine
                                                  return (c : l)

2 Responses to “Exercise 3.1”

  1. Jonathan Gallagher says:

    I think that myGetLine would be better if instead of comparing c with n, you compare it with something that denotes the end of a line. So maybe something like if ord c == 10
    (10 represents \n or the line feed).
    You could also do
    if isControl c
    and that would do the same thing in most cases, except it would handle any nonprinting characters such as hitting tab.

  2. admin says:

    Aha. I think somehow the formatting was broken – I’ve fixed it. The ‘n’ should be ‘\n’. Thanks.

Leave a Reply