Juan Figueira
Juan Figueira

Reputation: 161

Error on my Haskell code

I get this error on my code:

"parse error (possibly incorrect indentation or mismatched brackets)"

max3 a b c = if a>b && a>c then show a
     else if b>a && b>c then show b
     else if c>a && c>b then show c

i need to get the higher number between a, b and c

EDIT: After adding the else clause as suggested:

max3 a b c = if a>b && a>c then show a
     else if b>a && b>c then show b
     else if c>a && c>b then show c
     else show "At least two numbers are the same"

now i get this error " parse error on input `if' "

EDITED as suggested!

EDITED: SOLVED, i did with guards like said! Ty!

Upvotes: 0

Views: 125

Answers (2)

Boyd Stephen Smith Jr.
Boyd Stephen Smith Jr.

Reputation: 3202

import Data.List (maximum)

max3 a b c = maximum [a, b, c]

Fuhgeddaboudit.

Upvotes: 1

shree.pat18
shree.pat18

Reputation: 21757

As John L mentions in the comments, you need a final else clause to catch the case when none of your conditions is true.

Alternatively, you may use guards instead of if..else if, like so:

max3 a b c 
          | a > b && a > c = show a
          | b > a && b > c = show b
          | c > a && c > b = show c
          | otherwise = show "At least two numbers are the same"

Upvotes: 2

Related Questions