[Haskell-beginners] general structuring of "foreach" logic in IO

John M. Dlugosz ngnr63q02 at sneakemail.com
Mon Apr 14 15:41:20 UTC 2014

On 4/14/2014 4:34 AM, Patrick Wheeler wrote:
> @John
> That way you can write code that looks like:
> main = do
>      myargs <- getArgs
>      forM_ myargs $ \s -> do
>          putStrLn s
>          putStrLn $ "Second string" ++ s

That still introduces a throw-away name and extracts the args on another line.  Is there a 
way to do something like:

main = do
     forM_ (dereference getArgs) $ \s -> do ...

     mforM_ getArgs $ \s -> do ...

> You can read the discussion around om:
> http://www.reddit.com/r/haskell/comments/1i2zmq/a_useful_function_om/

Thanks, I will.

I guess it bothers me that "wrapped" values in the IO monad can't be independently used in 
situ, but need to be given a name on another line first.  Isn't the likes of "lift" and 
(even nicer) <*> supposed to address this idea?  I can't quite see why that doesn't work here.

(And that begs the question of why getArgs needs to be monadic in the first place.  It 
doesn't change its value; it's a strict constant at run-time, and not knowing it at 
compile time is my problem how?)


More information about the Beginners mailing list