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!!!