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:
isValid :: [Int] -> Bool
isValid [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13] = …
isValid _ = False
with …
the part you still need to fill in. Hint: you can use mod :: Integral a => a -> a -> a
to calculate a modulo (in some programming languages that is done with %
).
Here x13
is thus the last digit that you can use in the check.