[Haskell-cafe] Usage of . and $
schneegloeckchen at gmx.li
Wed Mar 7 04:35:37 EST 2007
On Tue, Mar 06, 2007 at 10:19:12PM +0000, David House wrote:
> 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
(f . g . h) x
will work, too.
> 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
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe