4 ms·
>but what's wrong with the syntax? I'll just link to this blog about readable Haskell. If everyone followed his advise, it would be much better. http://www.ha
by olah_1 7y ago
>but what's wrong with the syntax?
I'll just link to this blog about readable Haskell. If everyone followed his advise, it would be much better.
http://www.haskellforall.com/2015/09/how-to-make-your-haskell-code-more.html http://www.haskellforall.com/2015/09/how-to-make-your-haskel...
- brianberns 7y agoThanks. 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)).]
- ghkbrew 7y ago> 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 ))
- brianberns 7y agoYou'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.
- herbstein 7y ago> 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.
- chrisdirkis 7y agoAlternatively, I learnt it as "of" (from a mathematical standpoint). "(g ∘ f)(x) " is "g of f of x", so g(f(x))
- phlakaton 7y agoHaskell 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.
- axilmar 7y agoI don't see the above better than print(even(4*2))
- brianberns 7y agoYeah, that's a small example, so it's fine either way. But larger examples with lots of parentheses can be overwhelming (see: Lisp).
- dmitriid 7y agoAwesome advice! Thank you for the link. Sadly, the author of “Haskell in production” breaks many of the rules in part 2 and code reads like ascii-art :(
- PeCaN 7y agoNote that this is to make Haskell more readable to non-Haskell programmers (which is a bit of a weird goal IMO). It is not for readable Haskell in general. Advice like "not using $" just makes code less readable if you're familiar with Haskell and is quite frankly just bizarre. I do not think you'll find many programmers, Haskell or otherwise, who find lots of nested parenthesis to be the paragon of readability.¹ If the goal is to show snippets of cool Haskell code to other people this article is… fine I guess? But I don't think it's good for much more. Not even for teaching Haskell, since people should probably learn the language as it is used instead of some arbitrary "more readable to non-Haskell programmers" version. Using do-notation is probably even counterproductive for teaching since it obscures what's actually going on. Honestly this sentiment from the Haskell community that Haskell is somehow bizarre and impenetrable to outsiders and needs to be somehow watered down so that normal people can understand it just feels extremely elitist and if anything only scares people away from Haskell. ― ¹ Lisp programmers aside, but even then in Lisp you're basically just drawing a tree and so the parenthesis kind of fade away, but in languages with infix syntax and operator precedence you can't ignore them.
- empath75 7y ago> Honestly this sentiment from the Haskell community that Haskell is somehow bizarre and impenetrable to outsiders They’re not inventing that from thin air. I can understand Haskell with a great deal of effort, but it certainly requires a great deal of effort. My wife is a non-programmer. She knows nothing about code. I could explain what the go code I write does in a few minutes and she could follow it with a minimum of hand waving. I don’t think I could explain any non trivial Haskell code in hours to even professional programmers
- mbrodersen 7y agoI have explained Haskell code in a few minuts to professional programmers and they got it. It took a bit longer to explain monads but it wasn't hours.
- mbrodersen 7y agoI have tried to teach C# to a smart non-programmers friend. That was really really hard. The idea of OO/classes just blew his mind.