flatronka
flatronka

Reputation: 1081

Haskell equation roots compiling error

The following algorithm is supposed to calculate the 2nd degree equation root, but compiling it produces a compiling error. I copied it from a tutorial.

2 roota.hs:5:20: parse error on input `='

roots (a,b,c) = 
        if d < 0 then error "sorry" else (x1, x2)
        where x1 = e + sqrt d / (2 * a)
            x2 = e - sqrt d / (2 * a)
            d = b * b - 4 * a * c
            e = - b / (2 *a)

Thanks for the answers.

Upvotes: 0

Views: 206

Answers (2)

ja.
ja.

Reputation: 4249

the where need not be on a line by itself; this works:

roots (a,b,c) = 
        if d < 0 then error "sorry" else (x1, x2)
        where x1 = e + sqrt d / (2 * a)
              x2 = e - sqrt d / (2 * a)
              d = b * b - 4 * a * c
              e = - b / (2 *a)

or even this:

roots (a,b,c) = 
        if d < 0 then error "sorry" else (x1, x2) where 
 x1 = e + sqrt d / (2 * a)
 x2 = e - sqrt d / (2 * a)
 d = b * b - 4 * a * c
 e = - b / (2 *a)

Upvotes: 1

Jan
Jan

Reputation: 11726

It's a problem with indentation. Try

roots (a,b,c) = 
         if d < 0 then error "sorry" else (x1, x2)
         where
           x1 = e + sqrt d / (2 * a)
           x2 = e - sqrt d / (2 * a)
           d = b * b - 4 * a * c
           e = - b / (2 *a)

See also a question about indentation and and an article from a Haskell Wikibook.

Upvotes: 5

Related Questions