[Haskell-cafe] Usage of . and $

David House dmhouse at gmail.com
Tue Mar 6 17:19:12 EST 2007

On 06/03/07, Nicolas Frisby <nicolas.frisby at gmail.com> wrote:
> Composition with (.) builds a function, but you eventually want an
> Int, so we can't just use (.), but we can come pretty close.
> (sum . IntMap.elems . IntMap.IntersectionWith (\x y -> x*y)
> queryVector) rationalProjection

Often written:

f . g . h $ x

This is often prefered to the alternative:

f $ g $ h $ x

As it's visually lighter, and involves less editing if you wanted to
get rid of the x (say, you were eta-reducing the expression).

As to why:

f . g . h . x

doesn't work, (.) can only compose two functions, but x is not a
function, it is a value, so you have to apply it to the composite
function f . g . h using the ($) operator or parentheses.

-David House, dmhouse at gmail.com

More information about the Haskell-Cafe mailing list