[Haskell-beginners] Good style on "." and "$" pipeline?

Brent Yorgey byorgey at seas.upenn.edu
Tue Aug 21 22:43:53 CEST 2012


On Tue, Aug 21, 2012 at 09:34:49PM +0100, Carlos J. G. Duarte wrote:
>    Hi. Often I see the following pattern to build function "pipelines":
> 
>      x = f1 . f2 $ fn arg
> 
>    They use the "$" just before the last function call. I never got much into
>    it: when to use the "." or the "$"? I think today I figured it out:
>    => "." is a function composition operator: used to "merge" functions;
>    => "$" is a function application operator: used to apply a function to its
>    arguments.
> 
>    If this reasoning is correct, I think the following should be a more
>    adequate pattern:
> 
>      x = f1 . f2 . fn $ arg
> 
>    To merge/compose all functions first and apply the composed function to
>    its parameter(s). 
>    Am I correct on this? Thx

Yes, you are absolutely correct.  But sometimes people do something
like

  f1 . f2 $ fn arg

if for some reason they are thinking of "fn arg" as a "primitive"
starting point, and then applying a pipeline of functions f2, f1 to
that.  But it is really just a different point of view.  In any case,
both (f1 . f2 . fn $ arg) and (f1 . f2 $ fn arg) are perfectly good
style.  Having more than one $, like (f1 $ f2 $ fn $ arg), is frowned
upon.

-Brent



More information about the Beginners mailing list