<div dir="ltr">@John<div><br></div><div>It looks like you are looking for `forM_` from Control.Monad:</div><div><a href="http://hackage.haskell.org/package/base-4.7.0.0/docs/Control-Monad.html#v:forM_">http://hackage.haskell.org/package/base-4.7.0.0/docs/Control-Monad.html#v:forM_</a><br>
</div><div><br></div><div>That way you can write code that looks like:</div><div><br></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">main = do</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">
<span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"> myargs <- getArgs</span><br style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"> forM_ </span><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">myargs $ </span><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">\s -> do</span></div>
<div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"> putStrLn s</span></div></div><div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"> putStrLn $ "Second string" ++ s</span></div>
</div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px"><br></span></div><div><span style="color:rgb(0,0,0);font-family:arial,sans-serif;font-size:13px">This is just `mapM_` with the its two parameters fliped. </span><span style="color:rgb(0,0,0);font-family:arial,sans-serif">`forM_` is defined in a an idiomatic fashion, with the `flip`(prelude function) function.</span></div>
<div><font color="#000000" face="arial, sans-serif"><br></font></div><div><font color="#000000" face="arial, sans-serif">Definition of forM_</font></div><div><font color="#000000" face="arial, sans-serif"><a href="http://hackage.haskell.org/package/base-4.7.0.0/docs/src/Control-Monad.html#forM_">http://hackage.haskell.org/package/base-4.7.0.0/docs/src/Control-Monad.html#forM_</a><br>
</font></div><div><font color="#000000" face="arial, sans-serif"><br></font></div><div><font color="#000000" face="arial, sans-serif">Magnus solution has been used so often by some people that some have created idioms around it.</font></div>
<div><br></div><div>
<p class="">om f m x <span class="">=</span> m <span class="">>>=</span> flip f x </p></div><br>main = do<br> om forM_ getArgs $ \s -> do<br> putStrLn s<br> putStrLn $ "Second string: " ++ s<div>
<br></div><div>You can read the discussion around om:</div><div><div><a href="http://www.reddit.com/r/haskell/comments/1i2zmq/a_useful_function_om/">http://www.reddit.com/r/haskell/comments/1i2zmq/a_useful_function_om/</a><br>
</div></div><div><br></div><div>Hope that helps.</div><div><br></div><div>Patrick</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Sun, Apr 13, 2014 at 1:48 AM, Magnus Therning <span dir="ltr"><<a href="mailto:magnus@therning.org" target="_blank">magnus@therning.org</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Sat, Apr 12, 2014 at 08:29:09AM -0500, John M. Dlugosz wrote:<br>
> This works:<br>
><br>
> main = do<br>
> myargs <- getArgs<br>
> mapM_ (\s -> putStrLn s ) myargs<br>
><br>
> and imagine that the "body" will be substantial rather than just a putStrLn.<br>
> My gut instinct is that the code ought to be arranged as:<br>
><br>
> <any needed keywords or punctuation> and <the collection of items><br>
> <body to perform for every element<br>
> ...<br>
> ...<br>
> ><br>
><br>
> Meanwhile, there is no need to name the result of getArgs into myargs.<br>
><br>
> So, getArgs is of type IO [String], and I want to apply that in the<br>
> manner of a list. Without the Monad wrappers, plain<br>
> map ( blah ) strings<br>
> could be ( blah ) <$> strings, and in this particular case I don't<br>
> see a reversed-arg version, although there is one for <*> (as <**>).<br>
> But, for monad stuff in general there are reversed arrows for<br>
> (most?) everything, and that's where I'm heading.<br>
><br>
> So the first question is, how do I do the equivalent<br>
> map-as-nondeterministic-apply when the strings is further wrapped in<br>
> IO, as is the function being applied.<br>
><br>
> getArgs >>= mapM_ (\s -> putStrLn s )<br>
><br>
> does double-duty of moving the argument from last place to the left,<br>
> as it makes use of eta reduction. Because I have two things going<br>
> on (list applicative and IO monad) I'm losing the slickness of using<br>
> applicative syntax. Is there a nice way to make these work<br>
> together?<br>
><br>
> And more generally, how would you write such a construct? I'm<br>
> naturally biased with my knowledge in other languages, so maybe<br>
> there's a completely different "normal" way of approaching this?<br>
<br>
</div></div>I'm not entirely sure I get what you are asking for, but I'll take a<br>
stab and just let me know if I'm completely off the mark.<br>
<br>
If all you want is keep the 'string generator' on the right-hand side,<br>
then you have (=<<):<br>
<br>
mapM_ putStrLn =<< getArgs<br>
<br>
Personally I often like keeping the 'string generator' on the left<br>
(i.e. using (>>=) because when the expression to be mapped grows it<br>
allows this structuring of the code:<br>
<br>
getArgs >>= mapM_ $ \ s -> do<br>
...<br>
<span class="HOEnZb"><font color="#888888"><br>
/M<br>
<br>
--<br>
Magnus Therning OpenPGP: 0xAB4DFBA4<br>
email: <a href="mailto:magnus@therning.org">magnus@therning.org</a> jabber: <a href="mailto:magnus@therning.org">magnus@therning.org</a><br>
twitter: magthe <a href="http://therning.org/magnus" target="_blank">http://therning.org/magnus</a><br>
<br>
Perl is another example of filling a tiny, short-term need, and then<br>
being a real problem in the longer term.<br>
-- Alan Kay<br>
</font></span><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br><br clear="all"><div><br></div>-- <br>Patrick Wheeler<br><a href="mailto:Patrick.John.Wheeler@gmail.com">Patrick.John.Wheeler@gmail.com</a><br><a href="mailto:Patrick.J.Wheeler@rice.edu">Patrick.J.Wheeler@rice.edu</a><br>
<a href="mailto:Patrick.Wheeler@colorado.edu">Patrick.Wheeler@colorado.edu</a>
</div>