
Reading Simple Haskell - Tehnix
https://soupi.github.io/rfc/reading_simple_haskell/
======
Tehnix
Unrelated: 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](https://a-tour-of-go-in-
haskell.syocy.net/en_US/index.html).

~~~
junkscience2017
Go is impoverished compared to Haskell but not when it comes to concurrency.
The Haskell analogues for these concurrency primitives seem laborious...and
they were even moreso prior to the advent of the `async` library.

~~~
Tehnix
A commenter on /r/haskell[0] noted that there is actually a library that
mimics Go Channels a bit closer, called GoChan[1]. That said, I think `async`
is the idiomatic choice, so it probably makes more sense to keep it.

[0]
[https://www.reddit.com/r/haskell/comments/7ilhb9/a_tour_of_g...](https://www.reddit.com/r/haskell/comments/7ilhb9/a_tour_of_go_in_haskell/)

[1]
[https://hackage.haskell.org/package/gochan-0.0.2/docs/Contro...](https://hackage.haskell.org/package/gochan-0.0.2/docs/Control-
Concurrent-GoChan.html)

------
_0ffh
This site breaks the zoom function in Firefox.

Please 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!

~~~
quadrangle
While I agree STRONGLY, I just tested after seeing your comment and it led me
to try the Reader mode in FF which worked perfectly and thus allows full
zooming of this site.

~~~
_0ffh
Wow, thank you for introducing me to that feature!

------
weavie
Haskell can be very readable and elegant, but what I do find frustrating is
when research papers show sample Haskell, but it's not quite Haskell. They
just can't seem to resist the urge to replace various operators and types with
their own non-alphanumeric symbols, thus making what should be reading simple
code into some kind of hieroglyphic translation exercise..

~~~
nybble41
> when research papers show sample Haskell, but it's not quite Haskell

For the most part the code in the research papers actually _is_ valid Haskell,
provided you enable the -XUnicodeSyntax extension and include a few extra
modules. Haskell 98 permits Unicode symbols and identifiers; the extension
covers the handful of symbols which are built in to the language or enabled by
other extensions. You can find a full list here:
[https://downloads.haskell.org/~ghc/latest/docs/html/users_gu...](https://downloads.haskell.org/~ghc/latest/docs/html/users_guide/glasgow_exts.html#unicode-
syntax)

Personally I prefer Unicode symbols and tend to use them whenever possible. I
have some XCompose rules configured to make them easier to type.
Unfortunately, I have yet to find a comparable input system for Windows.

------
culturedsystems
One 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.

~~~
masklinn
> Haskell is indentation-sensitive.

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....](https://www.haskell.org/onlinereport/haskell2010/haskellch2.html#x7-210002.7)

[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

~~~
seventydwarves
Internally, Python is a lot like that, too - like in Haskell, the lexer adds
in brace tokens when the code is indented an unindented, which keeps the
grammar simpler. I guess the main difference here is that Haskell exposes the
underlying whitespace-insensitive notation to the programmer, too, whereas
Python keeps it hidden.

~~~
Tarean
Haskell is also a LOT more lenient. So stuff like

    
    
        if x < 5
        then 42
        else x
           + 1
           + 2
    

is legal.

------
hellofunk
I 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 same. But when I tried it with the same input
as originally, it was a different return result. This took a while to figure
out, until I realized that emacs changed the indentation of just one line,
because the author had apparently not followed the typical idioms or
conventions of indenting python in some way. That merely opening, beautifying,
and saving a file could change program output with no errors has turned me off
of white space sensitive syntax ever since.

Which is partly why OCaml gets some bias from me over Haskell.

~~~
Someone
You used a buggy beautifier that mishandled some code, and you blame
significant whitespace?

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 {}.

~~~
userbinator
_Beautifying C, for example, also can be tricky in the presence of nested
comments_

C comments do not nest.

~~~
dllthomas
Since C99, you've been able to have end-of-line comments inside a block
comment.

------
swlkr
I've always been confused when reading Haskell snippets on the web, this
really cleared things up for me!

------
bachmeier
Haskell is one of the few major languages I haven't used much. I find this to
be very helpful. Sometimes I look at the Pandoc source and wonder what the
hell I'm looking at.

------
nigwil_
I like the content but:

I can't link to the slide because of the way these pages are defined:

slide 11: The presented argument order in the ASCII-art of Red, Blue, Green I
think should be Red, Green, Blue to match the usual initialism.

I could not find a way to submit a pull-request for this presentation, and my
search-fu on github failed to find the associated repo, and there were no
links I could find in the presentation to submit comments.

~~~
gilmi
Thanks! fixed.

You can find the document and repo here:

[https://github.com/soupi/rfc/blob/master/reading_haskell.md](https://github.com/soupi/rfc/blob/master/reading_haskell.md)

------
speps
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...

~~~
Tehnix
The error messages should have improved a bit with GHC 8.2[0], you might wanna
check that out if you aren't already using it. For _stack_ that'll mean using
the nightlies though (e.g. `resolver: nightly-2017-12-09`).

[0]
[https://twitter.com/raichoo/status/825045995490795521](https://twitter.com/raichoo/status/825045995490795521)

------
cygned

        x +- y = (x + x) - (y + y)
    

That's crazy.

I should learn Haskell (again).

~~~
mlevental
what exactly is crazy about this?

~~~
runeks
The simplicity with which a new operator is defined.

~~~
akhilcacharya
I think this is a downside. It just makes reading new code that uses esoteric
symbols defined elsewhere.

~~~
gilmi
This[0] operator glossary reviews the most used operators.

[0]: [https://haskell-lang.org/tutorial/operators](https://haskell-
lang.org/tutorial/operators)

------
mark_l_watson
Nice guide for learning to read Haskell code! You have one minor type:
"tree1123 :: IntTree

tree ="

~~~
gilmi
thanks! fixed.

------
notsgnik
nice guide

