0

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!

5
  • 3
    you need a final "else". Or you could use maximum [a,b,c] Commented Jul 18, 2014 at 4:10
  • Actually it is "At least two numbers are the same". Commented Jul 18, 2014 at 5:53
  • otherwise works with guards, not if...else Commented Jul 18, 2014 at 6:05
  • then why i get that parse error on my first case? x_x Commented Jul 18, 2014 at 6:36
  • Your current code loads fine for me. Maybe there's something else in your file that causes trouble? Commented Jul 18, 2014 at 7:57

2 Answers 2

2

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"
Sign up to request clarification or add additional context in comments.

4 Comments

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 "The numbers are the same" i added else and now appear this "parse error on input `if'"
Please add your updated code into your question. Also, did you try my updated code which uses guards?
The conditions are non-exhaustive. a=b=2, c=1. Always use otherwise.
i used otherwise and i still get error "parse error on input `if'"
1
import Data.List (maximum)

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

Fuhgeddaboudit.

2 Comments

i need to create the function, its an exercise that need to my class!
But, you did create the function. Why can you use show, &&, and >, all defined by the standard library (not the language) but you can't use maximum also defined by the standard library?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.