> The $ operator is definitely confusing in Haskell
I generally think of it as a sort of open-ended parentheses that encloses the rest of the line. Instead of a backward pipe. It means "evaluate everything after this character first then apply the preceding function to it" just like parentheses would.
print $ even $ 4 * 2
is equivalent to print ( even ( 4 * 2 ))
Re:Haskell in Production
by brianberns on Thursday October 17, 2019 @05:02PM
(#21286337)
You're right, and that's why it's usually called "function application" instead of "backward pipe". However, using $ as an operator is confusing because the associativity is ambiguous if you're not already comfortable with it.
> Does (g ∘ f)(x) mean g(f(x)) or f(g(x))?
The function composition operator can be vocalized as, in this case, "g after f". That is, apply the f and then g. Translating '∘' into 'after' is the easiest way to understand it.
Re:Haskell in Production
by chrisdirkis on Thursday October 17, 2019 @08:54PM
(#21287726)
Alternatively, I learnt it as "of" (from a mathematical standpoint). "(g ∘ f)(x) " is "g of f of x", so g(f(x))
Haskell provides the (g . f) notation as the standard, and also has (f >>> g) notation buried in one of its more advanced libraries if anyone finds the right-to-left reading too disorienting.
I don't see the above better than
print(even(4*2))
Re:Haskell in Production
by brianberns on Friday October 18, 2019 @09:45AM
(#21291291)
Yeah, that's a small example, so it's fine either way. But larger examples with lots of parentheses can be overwhelming (see: Lisp).
[Side note: The mathematical symbol for function composition is the circle operator, ∘, which is also confusing for the same reason. Does (g ∘ f)(x) mean g(f(x)) or f(g(x))? Personally, I can never remember. If math used F#'s >> and << composition operators instead, there'd be no doubt that f >> g sends the output of f to g, so (f >> g)(x) means g(f(x)).]