A few weeks ago someone came with a way of abusing the BlockArgument extension by pointing that
$ could be replaced in same case with
do. For example
example1 = map (+1) do [1..5] ++ [10..20]
is equivalest to
example2 = map (+1) $ [1..5] ++ [10..20]
do has the advantage of over
$ to be layout aware, it doesn’t span until the end of the expression and can be nested
exampleZip = zip do map (*10) [1..5] do map do (+1) do [1..5] ++ [10..20]
(compare with the parenthesis or
I initially was shocked;
do notation is not made for that, it is abusing the current syntax and shouldn’t be used. However, the more I am thinking of it the more I like it (even though I haven’t used it in production … yet).
The obvious case for
$ is, we are use to it and
do is for Monad so it is confusing. Moreover this is fluke, the fact that
do something when
something is not a Monad works is a GHC bug and might be fixed, youn shouldn’t rely on it.
The counter argument are , we are only used to
$ because we have seen it and most us might actually have taken some time to get use to it. Moreover,
$ on the same expression is frown upon and the dot police often requires you to change
a $ b $ c d to
a . b $ c d.
The main reason I don’t like
$ ... $ is because I am never sure of how it is parsed and its precedenc rule. This is solved by using
Anyway, the main argument for
do (beside the obvious layout benefit) is it is a keyword, not a mere function.
Being a keyword means that it is part of the language, does not generate anything at runtime.
‘$’ on the other hand is just a function, which needs to be erased and has special treatment from GHC so that it works as we expect.
So maybe we should embrace this new style (or have a proposal for a new extension replacing layout with parenthesis alltogether making the
do trick obsolete).