News for hackers. Stuff that matters.
Re:Haskell in Production by brianberns on Thursday October 17, 2019 @02:21PM (#21284940)
Thanks. That's a good blog. The $ operator is definitely confusing in Haskell. F# uses <| to indicate a backward pipe, which I find much more intuitive. For example:

    print <| even <| 4 * 2
This means: Take the result of 4 * 2 and send it to `even`, then take that result and send it to `print`. Or you can write it with forward pipes to make it even clearer:

    4 * 2 |> even |> print
The thing about backward pipes is that they're often used to avoid parentheses, which is IMHO a good idea. It's just that thinking of them as "function application" is confusing. F#'s operators makes the data flow clear.

[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)).]

Re:Haskell in Production by ghkbrew on Thursday October 17, 2019 @04:56PM (#21286294)
> 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.
Re:Haskell in Production by herbstein on Thursday October 17, 2019 @06:22PM (#21286885)
> 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))
Re:Haskell in Production by phlakaton on Thursday October 17, 2019 @11:58PM (#21288541)
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.
Re:Haskell in Production by axilmar on Friday October 18, 2019 @03:33AM (#21289366)
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).