[Haskell-cafe] Re: Map list of functions over a single argument

Gene A yumagene at gmail.com
Wed Feb 21 14:59:29 EST 2007


On 2/21/07, Jules Bean <jules at jellybean.co.uk> wrote:
>
> Gene A wrote:
>
> > Prelude> let revApply a f = f a
> > Prelude> let rMap a fs = map (revApply a) fs
> > Prelude> rMap 2 [(*4),(^2),(+12),(**0.5)]
> > [8.0,4.0,14.0,1.4142135623730951]
> >
>
> Note that revApply here is precisely flip ($).
>
> And ($a) is the same as flip ($) a.
>
> So this reduces to one of the earlier examples rather quickly.
>
> It is possible to argue 'it's nice to give revApply a name'. It's also
> possible to argue 'taking a section of $ is even better than naming
> revApply'.

-----------------
jules,
.. right on... ran this through ghci...

 let rMap a fs = map ($ a) fs
{ that is clean ... gotta admit.. }
Prelude> rMap 2 [(*4),(^2),(+12),(**0.5)]
[8.0,4.0,14.0,1.4142135623730951]
Prelude> :t rMap
rMap :: forall a b. a -> [a -> b] -> [b]
====
About naming the secondary revApply function would agree and that would have
been in a "where" clause inside the definition of rMap had that been saved
to a file, but ghci doesn't really lend itself to multiline definitions so
that is why that was there, and it was also named in this case to show what
was going on... The functions as I originally defined them are probably
easier for someone new to Haskell to understand what was going on than the
rather stark ($ a) in the final factoring of the function... Though the
final resulting function is far the cleaner for that notation!

gene
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070221/e8056c97/attachment.htm


More information about the Haskell-Cafe mailing list