When I define a function and I use do to string functions together, how does it know when my list of functions has come to an end? For example: foo = do bar baz other_func bar = ... How does it know to stop at other_func? Thanks, Rob Hoelz
On 2/15/07, Rob Hoelz <hoelz@wisc.edu> wrote:
When I define a function and I use do to string functions together, how does it know when my list of functions has come to an end? For example:
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. -- Sebastian Sylvan +46(0)736-818655 UIN: 44640862
On 2/15/07, Sebastian Sylvan <sebastian.sylvan@gmail.com> wrote:
On 2/15/07, Rob Hoelz <hoelz@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
Hi Rob,
When I define a function and I use do to string functions together, how does it know when my list of functions has come to an end?
Simple answer: the identation. Long answer: read the Haskell 98 report specifically the bits on the layout rule. Usually just remembering that indentation is significant is enough for everything to "just work" Thanks Neil
On Feb 15, 2007, at 4:15 PM, Rob Hoelz wrote:
When I define a function and I use do to string functions together, how does it know when my list of functions has come to an end? For example:
foo = do bar baz other_func bar = ...
How does it know to stop at other_func?
Thanks, Rob Hoelz
'do' blocks are subject to the layout rule. See: http://en.wikibooks.org/wiki/Haskell/Indentation The short version is that it notices when a line is less indented, and that ends the do block. Rob Dockins Speak softly and drive a Sherman tank. Laugh hard; it's a long way to the bank. -- TMBG
participants (5)
-
Aaron McDaid -
Neil Mitchell -
Rob Hoelz -
Robert Dockins -
Sebastian Sylvan