[Haskell-cafe] newbie - IO issues (of course)
Stepan Golosunov
stepan at golosunov.pp.ru
Sat Jun 24 02:37:04 EDT 2006
On Sat, Jun 24, 2006 at 02:38:31PM +1000, Geoffrey King wrote:
> I have been work my way through "Haskell The Craft of Functional
> Programming", all was fine until IO (chapter 18). That is causing me
> bafflement.
>
> I am trying to write a totally trivial program that reads a series of
> integers from the console until it gets a zero, then returns the series
> of integers as a list.
>
> I am down to randomly editing my code, never a good sign. Any clues
> would be appreciated, My code so far, which clear doesn't work, is:
> getInt :: IO Int
> getInt = do
> putStr "Enter number (zero to quit)"
> line <- getLine
> return (read line :: Int)
>
> anIntList :: [Int]
anIntList is an IO action which reads a list, and as such has type
IO [Int], not [Int].
> anIntList =
> do
> let n = getInt
You should use "n <- getInt" to perform getInt and read integer into n.
"let n = getInt" just says that n is an IO action which reads integer,
not that integer itself.
> if n == 0
> then return []
> else return (n : anIntList)
As anIntList is not a list this won't work. You should perform anIntList
and then use its result:
do
list <- anIntList
return (n : list)
More information about the Haskell-Cafe
mailing list