I have the following, and it works, but I am trying teach myself Haskell, and I have the suspicion that my solutions is both inefficient and graceless. Any feedback would be appreciated.Trent.------------------------------------ {-8.The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds as follows:* consider each digit as a separate number;* moving left, double every other number from the second last;* subtract 9 from each number that is now greater than 9;* add all the resulting numbers together;* if the total is divisible by 10, the card number is valid.Define a function luhnDouble :: Int -> Int that doubles a digitand subtracts 9 if the result is greater than 9.For example:> luhnDouble 36> luhnDouble 63Using luhnDouble and the integer remainder function mod, define a functionluhn :: Int -> Int -> Int -> Int -> Boolthat decides if a four-digit bank card number is valid.For example:> luhn 1 7 8 4True> luhn 4 7 8 3FalseIn the exercises for chapter 7 we will consider a more general version of this function that accepts card numbers of any length.Hutton, Graham. Programming in Haskell (pp. 45-46). Cambridge University Press. Kindle Edition.-}luhnDouble :: Int -> IntluhnDouble x = if (2 * x) > 9then (2 * x) - 9else 2 * xluhn :: Int -> Int -> Int -> Int -> Boolluhn x1 x2 x3 x4 = if 0 == sum[luhnDouble x1, x2, luhnDouble x3, x4] `mod` 10then Trueelse False
_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners