[Haskell-beginners] Clarifying $ vs parentheses
Francesco Ariis
fa-ml at ariis.it
Thu Aug 20 19:40:31 UTC 2020
Hello Josh,
il 20 agosto 2020 alle 22:02 josh friedlander ha scritto:
> i understand that in general $ is a) right-associative and b)
> lowest-priority. but if so shouldn't these two be roughly the same?
>
> λ take (succ 10) $ cycle "hello world"
> "hello world"
>
> But not this?
> λ take $ succ 10 $ cycle "hello world"
>
> […]
λ> :info ($)
($) :: (a -> b) -> a -> b -- Defined in ‘GHC.Base’
infixr 0 $
So, since `$` is right associative, the expression
take $ succ 10 $ cycle "hello world"
becomes
take (succ 10 (cycle "hello world"))
`cycle "hello world"` makes sense, `succ 10` makes sense,
`succ 10 anotherArgument` does not.
Even `take someStuff` is probably not what you want, since take is usually
invoked with two arguments.
A useful intuition when you see ($) is `it will evaluate everything on
the right of it first`. This way, `not $ xx yy zz` looks right,
`take $ xx aa yy qq` less so.
Does this help?
—F
More information about the Beginners
mailing list