[Haskell] How does "do" know when to stop?

Aaron McDaid aaron at aaronmcdaid.com
Thu Feb 15 19:07:01 EST 2007


On 2/15/07, Sebastian Sylvan <sebastian.sylvan at gmail.com> wrote:
> On 2/15/07, Rob Hoelz <hoelz at wisc.edu> wrote:
> > foo = do
> >     bar
> >     baz
> >     other_func
> > bar = ...
> >
> > How does it know to stop at other_func?
>
> The "offside rule". Bar starts on the same column as foo so it's a new
> definition rather than belonging to foo.

If you don't quite trust the indentation you can use '{' and '}'
instead. When do is followed by '{' it uses ';' to separate lines
instead of newlines. This is equivalent to the above:

 foo = do {
     bar ; baz ;
other_func ;
  }
 bar = ...

Although I would rarely use this. ';' can also be used in other
places, such as separating the alternatives in a case statement. I
really like the fact that Haskell can mix and match the indentation
and brace styles - something other languages should try to pick up.

Aaron


More information about the Haskell mailing list