Re:Reading Simple Haskell
by masklinn on Saturday December 09, 2017 @03:38PM
(#15887977)
> Not using any indentation or newlines seems pretty extreme.
You can certainly use indentation and newlines, my examples were just demonstrating the ability to "obviously" not invoke layout since there's nowhere layout information can be.
Whether to use layout (braceless) or explicit structuring (braceful) is solely determined by a brace being present after a listed keyword, you can have any whitespace you want around it, and you can make that decision on a keyword-by-keyword basis[0] although it will apply to the entire block, so you can write
module Main where -- no brace, layout rules
main = do { -- brace, no layout
let { a = 3 };
let { b = 5 };
print $ a + b;
}
Now here the let statement (rather than expression) is a bit tricky because if we don't use the braces the ; will bind to the "let" which is not using layout, resulting in a parse error. An alternative — and the way do blocks normally desugar — is to use ; as a separator prefix, a style also used by e.g. Elm:
module Main where
main = do
{ let a = 3
; let b = 5
; print $ a + b
}
[0] this is very very useful when generating code which must surround user-provided code: the generated code can be braceful and the user-provided code can independently use layout without conflicts.
You can certainly use indentation and newlines, my examples were just demonstrating the ability to "obviously" not invoke layout since there's nowhere layout information can be.
Whether to use layout (braceless) or explicit structuring (braceful) is solely determined by a brace being present after a listed keyword, you can have any whitespace you want around it, and you can make that decision on a keyword-by-keyword basis[0] although it will apply to the entire block, so you can write
Now here the let statement (rather than expression) is a bit tricky because if we don't use the braces the ; will bind to the "let" which is not using layout, resulting in a parse error. An alternative — and the way do blocks normally desugar — is to use ; as a separator prefix, a style also used by e.g. Elm: [0] this is very very useful when generating code which must surround user-provided code: the generated code can be braceful and the user-provided code can independently use layout without conflicts.