ObinAtor 1
ObinAtor 1

Reputation: 38

Haskell multiple where statement

I don't get what's not valid about this

solveCubic a b c d = (-b/(3*a) - C/(3*a) - (b*b - 3*a*c)/(3*a*C), 0, 0)
    where Q = cbrt ((2*(b**3) - 9*a*b*c + 27*a*a*d)**2 - 4*((b*b - 3*a*c)**3))
          C = cbrt ((1/2)*(Q + 2*(b**3) - 9*a*b*c + 27*a*a*d))

The error is

    main.hs:19:11: error: Not in scope: data constructor `Q'
   |
19 |     where Q = cbrt ((2*(b**3) - 9*a*b*c + 27*a*a*d)**2 - 4*((b*b - 3*a*c)**3))
   |           ^

main.hs:20:11: error: Not in scope: data constructor `C'
   |
20 |           C = cbrt ((1/2)*(Q + 2*(b**3) - 9*a*b*c + 27*a*a*d))

Upvotes: 1

Views: 72

Answers (2)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477676

Variables start with a lowercase (or certain symbols, but that is not relevant here), type constructors, data constructors, type classes, etc. start with an uppercase.

You thus can fix this with:

solveCubic a b c d = (-b/(3*a) - e/(3*a) - (b*b - 3*a*c)/(3*a*e), 0, 0)
    where q = cbrt ((2*(b**3) - 9*a*b*c + 27*a*a*d)**2 - 4*((b*b - 3*a*c)**3))
          e = cbrt ((1/2)*(q + 2*(b**3) - 9*a*b*c + 27*a*a*d))

Upvotes: 3

sepp2k
sepp2k

Reputation: 370435

Variable names can't start with a capital letters. Identifiers starting with capital letters are reserved for constructor names (or type names when we're dealing with types rather than expressions).

So Q and C are seen as constructor names in your code, which is why the compiler complains that no constructors with those names have been defined. Name them q and c instead and the problem will go away.

Upvotes: 6

Related Questions