Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Haskell: Parse error in pattern

Tags:

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

like image 298
user905686 Avatar asked Dec 19 '11 13:12

user905686


1 Answers

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.

like image 148
kennytm Avatar answered Oct 03 '22 21:10

kennytm