I'm learning Haskell and I'm trying to implement the luhn algorithm.
I've created a helper function:
myMap :: (a -> b) -> (a -> b) -> [a] -> [b]
myMap p q [] = []
myMap p q [x] = [p x]
myMap p q (x : y : xs) = p x : q y : myMap p q xs
This function works fine. If I try to run it as myMap (*2) (+0) [1,2,3,4] it returns [2,2,6,4] which is the expected result.
Than I'm trying to implement the luhn algorithm:
luhn :: [Int] -> [Int]
luhn [x] = myMap (*2) (+0) [x]
This is part of the algorithm, I'll change it to return a Bool, but I'm trying to run as this is and it gives me:
*** Exception: main.hs:92:1-30: Non-exhaustive patterns in function luhn
Why is this happening?
luhn xs = myMap (*2) (+0) xsWhich in turn can be simplified asluhn = myMap (*2) (+0)(and I would prefer to writeidinstead of(+0), but that makes no difference). - Robin Zigmond