Live data from Hacker News

Reading Simple Haskell

soupi.github.io

21–30 of 82 posts

Re: Reading Simple Haskell

#24
post #19

Earlier quoted context omitted.

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???

Because braces don't introduce bugs? You can just as easily omit braces in a if/for etc statement and have a bug, or close the wrong brace at the wrong point (and also introduce a bug).

>omit braces

so you're saying braceless ifs and fors are bad? yes I agree.

> close the wrong brace

-/+ 1 flipping the right number of braces to get that kind of bug is much much harder than indenting a block incorrectly

Re: Reading Simple Haskell

#25
post #16

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 sa…

I'm not entirely sure if you are implying that wrong indentation in Haskell will result in programs that compile, but give differing output? I have honestly never seen that happen, and couldn't imagine a piece of code that would compile in either cases. EDIT: 1) as masklinn mentions, the indentation in Python and Haskell works differently. Furthermore, Python can have expressions as top-level, which Haskell cannot, a…

This example is very contrived, but challenge accepted..

    main = do
      let print = putStr . (++ "  ") in do
        print "happy"
        print "holidays" -- indentation sensitive
      putStrLn "!"
De-indenting the marked line by 2 spaces exposes some scroogey irony.

(Note that compiler warnings give a hint about the bad idea in this code, but nested do's can have this problem without warnings also.)

Re: Reading Simple Haskell

#27
post #23

x +- y = (x + x) - (y + y) That's crazy. I should learn Haskell (again).

`+-` here is an entirely new operator though. The shuffling of arguments might look a bit weird though, it could also be written

    (+-) x y = (x + x) - (y + y)
Symbol operators are automatically infix though, and you can make them normal using (), same as backticks make normal functions infix, `a la 2 `plus` 3.

Re: Reading Simple Haskell

#29

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.

> 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-…

I’d honestly appreciate some examples of non-indentation syntax. They’re think on the ground.

Re: Reading Simple Haskell

#30
post #29

Earlier quoted context omitted.

> 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-…

I’d honestly appreciate some examples of non-indentation syntax. They’re think on the ground.

It's as noted above: put an opening brace after the "where", "let", "do" and "of" keywords[0], put semi-colons between (or after both work) "statements" in a given "scope" e.g. declarations (in let and where blocks), clauses (in case/of blocks) and actual statements (in do blocks)

    module Main where {
    main = do {
    let {
    a :: Int; a = 8;
    b :: Int; b = 3;
    };
    let { c = a `quot` b } in print c;
    }}
You can also format it all on one line, it'll work just fine:

    module Main where { main = do {let {a :: Int; a = 8; b :: Int; b = 3;}; let { c = a `quot` b }; print c}}
[0] basically every keyword which opens a non-expression "body", a brace after `case` or `in` is a compilation error as they can only contain a single expression
Post reply on HN