I just started functional programming, using Haskell, and I want to write a short function that checks a 13-digit code and checks if it is an ISBN number.
The formula for the check is:
x13 = (10 − ((x1 + 3x2 +x3 + 3x4 +x5 + 3x6 +x7 + 3x8 +x9 + 3x10 +x11 + 3x12)%10))%10
(x1 being the first digit, x2 the second, ..., x13 the last digit etc.)
I want the input to be a list so it's easier for me (13 integers, each 0-9).
So something like this (stuff below is simplified):
isValid :: [Int] -> Bool
--isValid = True if (lastdigit = formula) -- can this be done in one (long) line?
So, for example:
isValid [ 9, 7, 8, 0, 1, 3, 7, 0, 5, 3, 4, 6, 9 ]
should return True
I've been trying to do this for a few hours but I'm not good enough at Haskell yet and it's confusing me. Can someone point me in the right direction? I don't know much about Haskell, which is the main problem.
You can just pattern match on a list of 13 elements, like:
with
…the part you still need to fill in. Hint: you can usemod :: Integral a => a -> a -> ato calculate a modulo (in some programming languages that is done with%).Here
x13is thus the last digit that you can use in the check.