[Haskell-cafe] $ do?
roma at ro-che.info
Wed Dec 15 08:36:55 CET 2010
* Jonathan Geddes <geddes.jonathan at gmail.com> [2010-12-14 19:59:14-0700]
> Quick question:
> Why do I need the $ in the following bits of code?
> > main = withSocketsDo $ do
> > --do something with sockets
> > foo = fromMaybe 0 $ do
> > --do something in the maybe monad
> I don't see (after admittedly only a minute or so thinking about it)
> where any grammar ambiguities would be if 'do' had an implicit $ in
> front of it:
> > foo = fromMaybe 0 do
> > --do something in maybe
> Though now that I've written it down, that is hard for me to visually
> parse at a glance. I'm still curious though.
it's not clear whether you ask how this implies from the standard or
what the rationale of such syntax is.
Regarding the former, there are two distinct syntactic categories in
Haskell: lexp and aexp. aexp produces all the constructs that can be
used as function arguments and does not include do expressions. lexp is
more broad and produces do expressions as well. Next look at these
productions to see the difference between function application and
operator application (sections 3.3 and 3.4 of 2010 report):
fexp → [fexp] aexp
inﬁxexp → lexp qop inﬁxexp
Regarding the rationale, I'm not so sure and I'd like to hear an
explanation from someone competent. But I assume it has something
to do with the fact that if you supply a 'do' argument, you cannot
supply any more arguments (because 'do' extends to the right as far as
possible). Not that I'm convinced that it is a valid reason to prohibit
Roman I. Cheplyaka :: http://ro-che.info/
Don't worry what people think, they don't do it very often.
More information about the Haskell-Cafe