Talk:Haskell/Solutions/Higher-order functions

Questions about solutions to 2. and 3.
Since I'm a complete beginner in Haskell, I'll leave here these two questions:

1) Isn't there a better way to define sequenceIO using foldl or foldr ?

2) Is the following paragraph correct? (to be placed after the solution to 3.)

Note the similarity between sequenceIO and mapIO. In fact, given sequenceIO as in 2., we could write mapIO as follows:

mapIO :: (a -> IO b) -> [a] -> IO [b] mapIO f list = sequenceIO (map f list) Although this gives the same result, it is a bit different from the first implementation. The first one is an IO action that when run, runs a list of IO actions (much like sequenceIO).

The second one will first evaluate an [IO b] (a list of IO actions), and then it can later be run as an IO action that runs each IO action in the list.

The second one lets the compiler/interpreter decide when it is the best time to evaluate each part of [IO b] (it won't run the IO b s out of order since sequenceIO won't allow that). However, this also means that this [IO b] list may be stored in memory for a long time, and each of your IO b objects could be huge. So in short: the second is better when memory is not an issue, and the first one is better otherwise.

-- 186.220.255.140 (discuss) 16:41, 8 March 2011 (UTC)