[Haskell-cafe] Newbie question on iterating over IO list

Taro Ikai taro at ikaisan.com
Mon Jun 19 23:02:44 EDT 2006

Here's a code fragment I have from working with Hal Daume's "Yet Another
Haskell Tutorial". I cannot figure out how to iterate over the result
from askForNumbers that returns "IO [Integer]", and print them.

The "<-" operator seems to take IO off of the results from actions like
getContents, which returns type "IO String", but I cannot seem to use it
to take IO off of lists with it. What am I supposed to do?

Also, as a side question, what should I be returning from the do function?


module Main

import IO

main = do
  hSetBuffering stdin LineBuffering
  s <- askForNumbers   {-- This compiles --}
  map show s           {-- But this doesn't. Why? --}
                       {-- What expression should I be returning from
do? --}

askForNumbers = do
  putStrLn "Give me a number (or 0 to stop):"
  guess <- getLine
  let number = read guess
  if number == 0
    then return []
    else do
      rest <- askForNumbers
      return (number : rest)


Thanks in advance.

Taro Ikai

More information about the Haskell-Cafe mailing list