[Haskell-cafe] Re: Very crazy

David Menendez dave at zednenem.com
Tue Sep 25 22:22:39 EDT 2007


On 9/25/07, Andrew Coppin <andrewcoppin at btinternet.com> wrote:
> This is why I found it so surprising - and annoying - that you can't use
> a 2-argument function in a point-free expression.
[...]
> I can't figure out why map . map works, but sum . zipWith (*) doesn't
> work. As I say, the only reason I can see is that the type checker hates
> me and wants to force me to write everything the long way round...

I suspect you're getting confused by the way Haskell treats
multi-parameter functions. Specifically, as far as function
composition is concerned, all Haskell functions have one parameter.

It may help to fully parenthesize function applications.

For example,
    \xs ys -> sum (zipWith f xs ys)
is
    \xs ys -> sum (((zipWith f) xs) ys)

The rule is that you can replace "f (g x)" with "(f . g) x". If you
apply that above, you get
    \xs ys -> (sum . ((zipWith f) xs)) ys
or
    \xs -> sum . zipWith f xs

Removing 'xs' takes a little more work, because it's nested more
deeply. We can rewrite the above as,

    \xs -> ((.) sum) ((zipWith f) xs)

Applying the rule f (g x) = (f . g) x gets us,

    \xs -> (((.) sum) . (zipWith f)) xs
or
    ((.) sum) . zipWith f
or
    (sum .) . zipWith f

The reason "map . map" is acceptible is that you can write "map (map f)".

-- 
Dave Menendez <dave at zednenem.com>
<http://www.eyrie.org/~zednenem/>


More information about the Haskell-Cafe mailing list