[Haskell-cafe] Statements spanning multiple lines?

Greg Buchholz haskell at sleepingsquirrel.org
Thu Dec 22 12:01:38 EST 2005


Daniel Carrera wrote:
> Hi all,
> 
> How do I write a statement that spans multiple lines?
> 
> I have this function:
> 
> pythagoras n = [(a,b,c) | a <-[1..n], b <-[1..n], c <-[1..n], a <= b, b 
> < c, a*a + b*b == c*c]
> 
> This should all be in one line. I know some ways to make the line 
> shorter, like defining local functions and using 'filter'. But the code 
> would be clearer if I could just make this one statement span several lines.

    Haskell's layout rules mean that you have to keep intenting at the
beginning of a line to continue a definition.  Try something like... 

pythagoras n = [(a,b,c) | a <- [1..n], 
                          b <- [1..n], 
                          c <- [1..n], 
                          a <= b, 
                          b <  c, 
                          a*a + b*b == c*c ]

...or...

pythagoras n = [(a,b,c) | 
  a <- [1..n], b <- [1..n], c <- [1..n],                        
  a <= b,      b <  c,      a*a + b*b == c*c ]

...See also...

http://www.haskell.org/onlinereport/lexemes.html#lexemes-layout


Greg Buchholz


More information about the Haskell-Cafe mailing list