0

having a bit of trouble with the following code. I keep getting an error message "saying parse error on input 'else' ". I cant see what im doing wrong, i have the else statement indented from the if and the syntax inside the statements are correct. Bit of background, its part of an insertion method for 2-3-4 trees, specifically for inserting where a fournode(Root3) needs to be split into its parent. x y and z are the values in the fournode while c1 - c4 are its child nodes. have looked at other questions on here about these types of errors but couldn't find anything useful :( Anyone have any ideas?

insert t (Root1 a left right) | t <= a = case left of
(Root3 x y z c1 c2 c3 c4) -> if t <= y then Root2 y a (insert t (Root1 x c1 c2) (Root1 z c3 c4) right else Root2 y a (Root1 x c1 c2) (insert t (Root1 z c3 c4)) right
_ -> Root1 a (insert t(left)) (right)

2 Answers 2

1

There may be other errors, but immediately I can see that your parenthesis aren't matched.... Take a look at the parens in the then. The compiler doesn't expect the else until after the then clause is finished, but the missing end paren is keeping this from happening.

Sign up to request clarification or add additional context in comments.

4 Comments

could you please explain? i dont see anything wrong with the parenthesis in this code. I should mention that this is only part of my insert method, but its the part thats not working. The code compiles fine and runs without it, but obviously as a result the fournode doesn't split properly
Count your parens, there are more opening than closing. In particular, after the then, I see an opening paren before insert, and although there are some open-and-close parens after, this particular one doesn't end before the else.
ok so i added the missing paren and that did the trick, but now im getting a similar error on the next line " _ -> Root1 a (insert t(left)) (right) " and i really cant see anything wrong with it? pretty sure my logic is correct at least :p
You need to put the same whitespace before each item in a case-of. That means that the (Root3 x y z c1 c2 c3 c4) -> and _ -> need to be indented to the same place, and they are not.
0

Your indentation has mixed tabs and spaces, which is a big no-no: indentation is very significant in Haskell, and you want to make sure that what you see is the same as what the compiler sees. Just because a tab looks about as wide as eight spaces doesn't mean you can put one in instead of the eight spaces.

I recommend you fix this to use spaces-only, make sure things are lined up correctly, and then if you're still having issues ask a new question.

Comments

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.