
Hi all; I'm new to the list, so I just wanted to say hi and -- of course -- ask a quick question: I have some IO actions that I want to map over a list of pairs -- these are just directory names and their down-cased versions. It wasn't difficult to actually get the right behavior by just doing mapM twice. -- putDirs just outputs something like "mv fst snd" mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs This bothered me though, because I suspected that this could be done in one pass. Naively I proceeded to this. mapM_ (putDirs >> (uncurry renameFile)) pairs Which didn't work. I thought about it a little more before realizing that putDirs wouldn't get any parameters this way: I needed some way to distribute the pair over both operations. Here's the higher-order function I needed: foo h f g i = h (f i) (g i) which could then be curried and we get: mapM_ (foo (>>) putDirs $ uncurry renameFile) pairs Works great. So my question: is there a established name for foo? What about foo partially applied to (>>)? This was a fun exercise, but I'd like to use the standard implementations if they exist. Thanks! -- Lou.

Hi Louis,
Try foo = liftM2, and check out the recent haskell thread "Cannot understand
liftM2".
Cheers, - Conal
On 12/12/06, Louis J Scoras
Hi all;
I'm new to the list, so I just wanted to say hi and -- of course -- ask a quick question:
I have some IO actions that I want to map over a list of pairs -- these are just directory names and their down-cased versions. It wasn't difficult to actually get the right behavior by just doing mapM twice.
-- putDirs just outputs something like "mv fst snd" mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs
This bothered me though, because I suspected that this could be done in one pass. Naively I proceeded to this.
mapM_ (putDirs >> (uncurry renameFile)) pairs
Which didn't work. I thought about it a little more before realizing that putDirs wouldn't get any parameters this way: I needed some way to distribute the pair over both operations. Here's the higher-order function I needed:
foo h f g i = h (f i) (g i)
which could then be curried and we get:
mapM_ (foo (>>) putDirs $ uncurry renameFile) pairs
Works great. So my question: is there a established name for foo? What about foo partially applied to (>>)? This was a fun exercise, but I'd like to use the standard implementations if they exist.
Thanks!
-- Lou. _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 12/12/06, Louis J Scoras
I have some IO actions that I want to map over a list of pairs -- these are just directory names and their down-cased versions. It wasn't difficult to actually get the right behavior by just doing mapM twice.
-- putDirs just outputs something like "mv fst snd" mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs
This bothered me though, because I suspected that this could be done in one pass. Naively I proceeded to this.
mapM_ (putDirs >> (uncurry renameFile)) pairs
Which didn't work. I thought about it a little more before realizing that putDirs wouldn't get any parameters this way: I needed some way to distribute the pair over both operations. Here's the higher-order function I needed:
foo h f g i = h (f i) (g i)
which could then be curried and we get:
mapM_ (foo (>>) putDirs $ uncurry renameFile) pairs
Works great. So my question: is there a established name for foo? What about foo partially applied to (>>)? This was a fun exercise, but I'd like to use the standard implementations if they exist.
Before we get too far down the obfuscation road, I'd like to offer what I think is more readable than a liftM2 solution: mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs I haven't tested it, but I hope that does the same thing. To me, this explicitely shows what each is doing, moreso than with a point-free 'foo' combinator. The way my mind worked to get to this solution: mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs ==> mapM_ (\p -> putDirs p) pairs mapM_ (\p -> uncurry renameFile p) pairs ==> mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs Is that a reasonable solution? Bryan Burgers

This rewrite changes the order of execution. The old version did all of the
putDirs and then all of the renameFile calls. The new one interleaves
putDirs & renameFile calls. In general, ">>" is not commutative, though in
this case you may be as happy with either order. - Conal
On 12/12/06, Bryan Burgers
On 12/12/06, Louis J Scoras
wrote: I have some IO actions that I want to map over a list of pairs -- these are just directory names and their down-cased versions. It wasn't difficult to actually get the right behavior by just doing mapM twice.
-- putDirs just outputs something like "mv fst snd" mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs
This bothered me though, because I suspected that this could be done in one pass. Naively I proceeded to this.
mapM_ (putDirs >> (uncurry renameFile)) pairs
Which didn't work. I thought about it a little more before realizing that putDirs wouldn't get any parameters this way: I needed some way to distribute the pair over both operations. Here's the higher-order function I needed:
foo h f g i = h (f i) (g i)
which could then be curried and we get:
mapM_ (foo (>>) putDirs $ uncurry renameFile) pairs
Works great. So my question: is there a established name for foo? What about foo partially applied to (>>)? This was a fun exercise, but I'd like to use the standard implementations if they exist.
Before we get too far down the obfuscation road, I'd like to offer what I think is more readable than a liftM2 solution:
mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs
I haven't tested it, but I hope that does the same thing. To me, this explicitely shows what each is doing, moreso than with a point-free 'foo' combinator.
The way my mind worked to get to this solution:
mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs
==>
mapM_ (\p -> putDirs p) pairs mapM_ (\p -> uncurry renameFile p) pairs
==>
mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs
Is that a reasonable solution?
Bryan Burgers _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On 12/12/06, Conal Elliott
This rewrite changes the order of execution. The old version did all of the putDirs and then all of the renameFile calls. The new one interleaves putDirs & renameFile calls. In general, ">>" is not commutative, though in this case you may be as happy with either order. - Conal
Right. That's really why I wanted to change it. It should do both actions for each element in one pass over the list.
On 12/12/06, Bryan Burgers
wrote:
Before we get too far down the obfuscation road, I'd like to offer what I think is more readable than a liftM2 solution:
mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs
I haven't tested it, but I hope that does the same thing. To me, this explicitely shows what each is doing, moreso than with a point-free 'foo' combinator.
The way my mind worked to get to this solution:
mapM_ putDirs pairs mapM_ (uncurry renameFile) pairs
==>
mapM_ (\p -> putDirs p) pairs mapM_ (\p -> uncurry renameFile p) pairs
==>
mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs
Is that a reasonable solution?
That works too. I don't know if I consider the liftM2 solution obfuscated yet, but you're right, it could be leaning that way. Thanks for the alternative suggestion. -- Lou.

On Tue, Dec 12, 2006 at 11:29:11AM -0500, Louis J Scoras wrote:
Which didn't work. I thought about it a little more before realizing that putDirs wouldn't get any parameters this way: I needed some way to distribute the pair over both operations. Here's the higher-order function I needed:
foo h f g i = h (f i) (g i)
foo is a special case of Control.Monad.liftM2.
import Control.Monad import Control.Monad.liftM2 mapM_ (liftM2 (>>) print print) [2,3,4,5] 2 2 3 3 4 4 5 5
(This is using the Monad ((->) a) instance) HTH, Stefan
participants (4)
-
Bryan Burgers
-
Conal Elliott
-
Louis J Scoras
-
Stefan O'Rear