Good layout style? (was: Re: "where" block local to a guard?)

Hal Daume III hdaume@ISI.EDU
Tue, 17 Sep 2002 22:22:38 -0700 (PDT)


I think this is purely a personal taste kind of thing.  First off, though,
only 'where', 'let', 'of' and 'do' induce layout.  I've seen many layout
styles; the most common seem to be:

  let x = ...
      y = ...
      z = ...
  in  e

which is probably a carryover from ML like languages where you might have
to say

  let x = ...
  let y = ...
  let z = ...
  in  e

sometimes "e" doesn't have two spaces before it and sometimes the "in" is
indented an extra space.

it also seems to be fairly common to put the "in" on the end of the last
line in the local definitions.

'where' seems to be less variable.  by far the most common of what i've
seen in:

  where x = ...
        y = ...

but i've also seen

  where
    x = ...
    y = ...

the advantage here is that its very easy to remove the 'x' definition
without having to reorder other things.

there seem to be two things people do with 'do'.  the first is obvious:

  do x ...
     y ...
     z ...

but another common one (esp in the ghc code) is:

myfunction = do
  x ...
  y ...
  z ...

which i've recently become fond of.

with case/of, the most common seems to be:

  case x of
    y   -> ...
    abc -> ...

but the indentations vary by user.

if/then/else seems to be highly variable.  i personally like

  if foo
    then bar
    else baz

but opinions are likely to vary widely.

likely, |{different offered layout options}| >= |{people who respond to
this email}|, though, so take anything with a grain of sand.

 - hal

--
Hal Daume III

 "Computer science is no more about computers    | hdaume@isi.edu
  than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume

On 18 Sep 2002, Dr Mark H Phillips wrote:

> On Wed, 2002-09-18 at 01:26, Hamilton Richards wrote:
> > You can get the effect you're after by using let-expressions:
> > 
> > >  functn :: Int -> Int
> > >  functn i
> > >      | i>5       = let t = functn (i-2) in t * i
> > >      | i>0       = let t = functn (i-1) in t * i
> > >      | otherwise = 1
> > 
> > 'where' is part of the syntax of definitions, not expressions. This 
> > enables a name defined in a where-clause to be used in more than one 
> > guarded expression.
> 
> Thanks for this!  It would seem "let ... in ..." is what I want.
> 
> But I'm a bit confused about how to use the off-side rule in
> conjunction with let.  Do I do:
> 
>     let a=1
>         b=2
>         c=3
>     in a*b*c
> 
> or do I do:
> 
>     let
>     a=1
>     b=2
>     c=3
>     in
>     a*b*c
> 
> or, in the context of a guard, do I do:
> 
>     | i>5      = let a=1; b=2; c=3
>       in a*b*c
> 
> Basically I'm a bit confused about how the offside rule
> works in various situations.
> 
> With "if ... then ... else ..." I don't know whether I should be doing
> 
>   f x = if x>5 
>     then x*x 
>     else 2*x
> 
> or
> 
>   f x = if x>5
>         then x*x
>         else 2*x
> 
> or
> 
>   f x = if x>5
>           then x*x
>             else 2*x
> 
> or what!
> 
> Hugs seems to think they are all legal.  Is there any rational as to
> how to do layout?  Any tips would be greatly appreciated!
> 
> Thanks,
> 
> Mark.
> 
> 
> _______________________________________________
> Haskell mailing list
> Haskell@haskell.org
> http://www.haskell.org/mailman/listinfo/haskell
>