23

Who likes to tell me what is wrong with this code (syntactically)?

-- merge two sorted lists
mergeX [] b res = b ++ res
mergeX a [] res = a ++ res
mergeX a:as b:bs res
    | a > b     = mergeX as b:bs a:res
    | otherwise = mergeX a:as bs b:res

Interpreter:

Parse error in pattern: mergeX

2 Answers 2

40

You need some parenthesis:

mergeX [] b res = b ++ res
mergeX a [] res = a ++ res
mergeX (a:as) (b:bs) res
    | a > b     = mergeX as (b:bs) (a:res)
    | otherwise = mergeX (a:as) bs (b:res)

The reason is because : has a lower precedence than function application, so mergeX a:as b:bs res will be parsed as:

(mergeX a):(as b):(bs res)

which is an error.

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

2 Comments

Thanks, I remember that I had sth. like that before ;). Do you normally always use (x:xs) instead of x:xs to prevent mistakes?
@user905686: In a pattern, yes.
2

You need to put constructor patterns (or however they are called) in parantheses.

mergeX (a:as) (b:bs) res 

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.