07.26.07

Exercise 3.1

Posted in Uncategorized at 12:13 pm by admin

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 Comments »

  1. Jonathan Gallagher said,

    December 13, 2008 at 2:51 pm

    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 said,

    December 14, 2008 at 6:51 pm

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

Leave a Comment