I only started Haskell since the 1st day of Advent of Code this year. I didn't see that mentioned much either in the article or other articles, but Haskell is like Python and has significant whitespace to determine scope. The error messages related to this aren't always clear...
Reading Simple Haskell
11–20 of 82 posts
Re: Reading Simple Haskell
#12Which is partly why OCaml gets some bias from me over Haskell.
Re: Reading Simple Haskell
#13Please be considerate towards the visually impaired, and don't break zoom on one of the few browsers that actually even have a somewhat useful zoom function!
Re: Reading Simple Haskell
#14One thing this doesn't mention, which might be useful to know, is that Haskell is indentation-sensitive. For instance, those examples using "let" won't compile if the lines beginning with "let" and "in" aren't indented. I'd be interested if anyone knows of a simple guide to Haskell's indentation rules - I still find it trips me up now and then.
That's not really true, at least not in the way Python is. Haskell is actually defined using "braceful" syntax, and that syntax can be used anywhere (and is useful for code generation); on top of that it has Layout Rules[0].
Basically:
Any time a "where", "let", "do" or "of" keyword is not followed by a brace, an implicit one is inserted and the indentation of the next lexeme (non-comment non-whitespace item) is noted[1]
For any subsequent line,
* if it's indented by the same amount as the one noted, a semi-colon is prepended
* if it's indented by less than the noted amount, a closing brace is prepended
A closing brace is also inserted when encountering an illegal lexeme (for the current structure) where a a closing brace would be legal.
[0] https://www.haskell.org/onlinereport/haskell2010/haskellch2....
[1] except if the indentation of the next non-whitespace lexeme is less than the current indentation level, then an empty block is inserted, and layout processing for the current scope continues
Re: Reading Simple Haskell
#15I once downloaded a python script, ran it with some input, and got a result. Curious, I opened the script in Emacs, which then opened it into python mode. As is my habit, I indented the whole file, which did basic white space changes to it according to python mode. I didn't realize I did this at first, and I saved and exited the script, then played around with it some more. Still got results, everything seemed the sa…
Re: Reading Simple Haskell
#16I once downloaded a python script, ran it with some input, and got a result. Curious, I opened the script in Emacs, which then opened it into python mode. As is my habit, I indented the whole file, which did basic white space changes to it according to python mode. I didn't realize I did this at first, and I saved and exited the script, then played around with it some more. Still got results, everything seemed the sa…
EDIT:
1) as masklinn mentions, the indentation in Python and Haskell works differently. Furthermore, Python can have expressions as top-level, which Haskell cannot, and you can also have if statements with no else in Python, which again, in Haskell you cannot.
2) I know of no formatters for Haskell which will change the semantics, since they all rely on rendering an AST of the code (IIRC).
3) Ocaml is also white-space sensitive, so I don't see the point in the comparison there?
4) You would need to have some pretty contrived code for it to both satisfy the parser and type-check and be wrong logic caused by moving an indentation.
Like, you can't do it in a function that's a if-then-else plainly, nor a case-statement. Maybe a where statement, but then you just promoted the function to top-level, which will not be a problem unless it's shadowing something else, for which you'll get a compiler warning.
Show me some code, and then I'll consider the point valid, I just can't come up with any examples.
Re: Reading Simple Haskell
#17I once downloaded a python script, ran it with some input, and got a result. Curious, I opened the script in Emacs, which then opened it into python mode. As is my habit, I indented the whole file, which did basic white space changes to it according to python mode. I didn't realize I did this at first, and I saved and exited the script, then played around with it some more. Still got results, everything seemed the sa…
For that to be valid criticism, it must be particularly hard to write a beautifier for languages with significant whitespace relative to other languages.
I don’t think it is. Beautifying C, for example, also can be tricky in the presence of nested comments (potentially of different types) and nested if statements, some without else clauses, some without {}.
Re: Reading Simple Haskell
#18Unrelated: I'm obviously quite biased, but I generally like Haskell code for pseudo code like things, because it does away with a lot of syntax specific cruft, and stays close to the math it's usually talking about. Also, readers might be interested in A Tour of Go in Haskell, which is the concurrency chapter of A Tour of Go, but just...in Haskell - https://a-tour-of-go-in-haskell.syocy.net/en_US/index.html .
Re: Reading Simple Haskell
#19I once downloaded a python script, ran it with some input, and got a result. Curious, I opened the script in Emacs, which then opened it into python mode. As is my habit, I indented the whole file, which did basic white space changes to it according to python mode. I didn't realize I did this at first, and I saved and exited the script, then played around with it some more. Still got results, everything seemed the sa…
honestly it's bananas. I can't for the life of understand how such a poor form over function decision was ever made by an engineer. the number of bugs (>0) this introduces into production code every year is reason enough (imho the only relevant metric) to prefer braces. are block delimiting braces really that onerous???
Re: Reading Simple Haskell
#20I once downloaded a python script, ran it with some input, and got a result. Curious, I opened the script in Emacs, which then opened it into python mode. As is my habit, I indented the whole file, which did basic white space changes to it according to python mode. I didn't realize I did this at first, and I saved and exited the script, then played around with it some more. Still got results, everything seemed the sa…