Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!snorkelwacker.mit.edu!apple!usc!wuarchive!bcm!dimacs.rutgers.edu!aramis.rutgers.edu!paul.rutgers.edu!njin!uupsi!sunic!dkuug!diku!thorinn From: thorinn@diku.dk (Lars Henrik Mathiesen) Newsgroups: comp.lang.functional Subject: Re: "Off-side rule" Message-ID: <1991Jan16.154302.4129@odin.diku.dk> Date: 16 Jan 91 15:43:02 GMT References: <22307@rouge.usl.edu> <1991Jan10.111559.12440@odin.diku.dk> <293@smds.UUCP> <7415@vanuata.cs.glasgow.ac.uk> Sender: news@odin.diku.dk (Netnews System) Organization: Department of Computer Science, U of Copenhagen Lines: 71 kh@cs.glasgow.ac.uk (Kevin Hammond) writes: >In article <293@smds.UUCP> rh@smds.UUCP (Richard Harter) writes: >>Could some one post a summary explanation of the off-side rule, please? >Simply put, in Miranda all text to the right of a defining operator >("where" or "=") after a newline comprises part of the previous line. >Other newlines act as separators. This applies recursively, so you >can build up levels of nested definitions structured by their layout. This is not quite correct. The offside rule in Miranda applies to certain nonterminals in the syntax such as: each alternative on the righthand side of a variable or function definition, other righthand sides (type definitions, aliases, and specifications), bodies of module inclusion directives. The definition list after "where" does not create a ``offside level'' in itself, it's included in that of the last alternative of the righthand side. Also, the critical indentation is determined by the first token produced by the nonterminal, not by the token before it. >This allows: > || Example 2 > f (x:xs) > y > z > = x + > y + z Therefore, this example gives a "syntax error - unexpected token OFFSIDE". The last line should be indented to the level of the "x" in the line above, not just past the "=". >Note that Haskell's layout rule is *not identical* to the Miranda >off-side rule. In particular: > i) It is possible to turn off layout at will; > ii) The layout rules do not use "=" as a defining point; > iii) Alignment of definitions is significant >(ii) is particularly important -- I find my Miranda programs regularly >vanish off the RHS of the page (because of the need to insert a fixed >amount of space), but this never happens with Haskell. (ii) is incorrect, and the wild indentation can be avoided with an alternative style: function of six rather longwindedly named arguments = right hand side where some other function = is defined You can also break before the "=", at the cost of a tabstop. I usually do that, especially when there are several alternatives: foo a b c = bar a , if b = c = baz (bar b ++ bar c) kvetch , otherwise where kvetch = burble a b c It's not pretty, but it always works (even if you modify the left hand side later) and the indentation only depends on nesting level. The other two points are valid, and very much in Haskell's favor. -- Lars Mathiesen, DIKU, U of Copenhagen, Denmark [uunet!]mcsun!diku!thorinn Institute of Datalogy -- we're scientists, not engineers. thorinn@diku.dk