I'm currently working through the book Programming in Haskell (which is absolutely amazing so far), but ran into a problem in exercise 4.8.8.
The task is to implement the Luhn algorithm in Haskell, using a help function luhnDouble :: Int -> Int
(which doubles a digit and subtracts 9 if the result is greater than 9) and the mod
funcion.
Implementing the luhnDouble
function was no problem, but I'm struggling to bring both of them into a function of type Int -> Int -> Int -> Int -> Bool
.
I have tried doing it two ways:
luhn :: Int -> Int -> Int -> Int -> Bool
luhn w x y z = if (((luhnDouble w) + x + (luhnDouble y) + z) mod 10) == 0 then True
else False
I receive a type error.
* Couldn't match expected type `(Integer -> Integer -> Integer)
-> Integer -> Integer'
with actual type `Int'
* The function `(luhnDouble w) + x + (luhnDouble y) + z'
is applied to two arguments,
but its type `Int' has none
In the first argument of `(==)', namely
`(((luhnDouble w) + x + (luhnDouble y) + z) mod 10)'
In the expression:
(((luhnDouble w) + x + (luhnDouble y) + z) mod 10) == 0
But am I not giving the functions 4 Int
s as arguments and getting a Bool
as a result?
I then tried currying the function and using lambda expressions:
luhn :: Int -> (Int -> (Int -> Bool))
luhn = \w ->(\x ->(\y -> (\z -> ((luhnDouble w) + x + (luhnDouble y) + z mod 10))))
But I'm not sure how to bring in the if
expression in here to get a Bool
value as a result.
Could anyone help me out, and give me a tip how I could solve this?