Fail to implement `++` in Haskell

Currenty I’m studying Learn You a Haskell For A Great Good. When I try to implement ++ function in Haskell, I meet a problem that I couldn’t solve.

Here I follow the book to declare my own List type and the .++ function :

infixr 5 :-:

data List a = Empty | a :-: (List a) deriving (Show, Read, Eq, Ord)

infixr 5  .++

(.++) :: List a -> List a -> List a
(.++) Empty xs = xs
(x :-: xs) ++ ys = x :-: (xs .++ ys)

Here’s the test I do :

λ> let a = 3 :-: 4 :-: 5 :-: Empty
λ> let b = 3 :-: 4 :-: 5 :-: Empty
λ> :t a
a :: Num a => List a
λ> :t b
b :: Num a => List a
λ> :t (.++)
(.++) :: List a -> List a -> List a

Everything looks good so far, but when I try to use (.++), Haskell warns me :

λ> a .++ b
*** Exception: section_6_Recursive_data_structure.hs:68:1-19: Non-exhaustive patterns in function .++

I don’t know why this happens, any advice would be of great help!!!

1 Like
(x :-: xs) ++ ys = x :-: (xs .++ ys)

You’re redefining (++) in the second case, rather than defining a new case for (.++). ^^

In general, you should probably compile your programs with -Wall, which enables warnings for (among others) non-exhaustive patterns like this and would have saved you here.
It’s a bit unfortunate that the default in GHC doesn’t include many warnings but that’s mostly for historical reasons.

1 Like

OH!!! I didn’t notice that! Thank you very much for the careful reading! I think maybe this shouldn’t be a question to ask since it’s just a typo. I’ll try to compile with -Wall. Thanks!

1 Like