[Haskell-cafe] Need feedback on my Haskell code

Ryan Ingram ryani.spam at gmail.com
Thu Jul 30 23:53:02 EDT 2009


On Wed, Jul 29, 2009 at 4:06 AM, Johan Tibell<johan.tibell at gmail.com> wrote:
> (map maySwitch . unfoldr go) (x1,y1,0)
>
> should work.

which is the same as
> map maySwitch (unfoldr go (x1,y1,0))

People have stylistic differences with ($) vs. (.); I would write it as

> map maySwitch $ unfoldr go $ (x1,y1,0)
(or, more likely)
> map maySwitch $ unfoldr go (x1,y1,0)

but some people like to make the function pipelines more explicit
(with the composition operator (.) instead of the application operator
($)).

Read ($) as a parenthesis that extends as far to the right as
possible; so you can write, for example:
> map (+1) $ map (*2) $ map (+3) [1,2,3]
which is the same as
> map (+1) (map (*2) (map (+3) [1,2,3]))
but without having to count how many parentheses you need on the right.

Due to the precedences of (.) and ($), you can use either

> blah $ blah2 $ blah3 $ something
or
> blah . blah2 . blah3 $ something

After inlining ($) and (.), the former is
> blah (blah2 (blah3 (something)))
whereas the latter is
> (\x -> blah ((\y -> blah2 (blah3 y)) x)) (something)
which beta reduces to the same thing:
=> blah ((\y -> blah2 (blah3 y)) (something))
=> blah (blah2 (blah3 (something)))

  -- ryan


More information about the Haskell-Cafe mailing list