How to resolve the algorithm Luhn test of credit card numbers step by step in the Haskell programming language
How to resolve the algorithm Luhn test of credit card numbers step by step in the Haskell programming language
Table of Contents
Problem Statement
The Luhn test is used by some credit card companies to distinguish valid credit card numbers from what could be a random selection of digits. Those companies using credit card numbers that can be validated by the Luhn test have numbers that pass the following test:
For example, if the trial number is 49927398716:
Write a function/method/procedure/subroutine that will validate a number with the Luhn test, and use it to validate the following numbers:
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Luhn test of credit card numbers step by step in the Haskell programming language
This code contains two implementations of the Luhn algorithm, a checksum formula used to validate credit card numbers. The first implementation, luhn
, takes a string representing a credit card number and returns a Bool
indicating whether the number is valid according to the Luhn algorithm.
The second implementation, also called luhn
, is equivalent to the first but uses a different approach. It uses the transpose
and chunksOf
functions from the Data.List
and Data.List.Split
libraries to separate the digits of the credit card number into odd and even positions. It then applies the Luhn algorithm to each set of digits separately and sums the results.
Here is a step-by-step breakdown of the first implementation:
-
The
map
function is used to apply theluhn
function to each element of the list of credit card numbers. Theluhn
function takes a string as input and returns aBool
indicating whether the string is a valid credit card number. -
The
luhn
function takes a string as input and converts it to a list of integers using thedigitToInt
function from theData.Char
library. -
The list of integers is then reversed using the
reverse
function. -
The
zipWith
function is used to pair up the elements of the reversed list with the elements of a cycle of the list[1,2]
. This cycle is created using thecycle
function from theData.List
library. -
The
map
function is used to apply theuncurry
function to each element of the list of pairs. Theuncurry
function takes a function of two arguments and returns a function of one argument that applies the function of two arguments to its argument. -
The
(
+)
function is a function that takes two numbers and returns their sum. -
The
divMod
function takes two numbers and returns a tuple containing the quotient and remainder of the division of the first number by the second number. -
The
map
function is used to apply thedivMod
function to each element of the list of pairs. -
The
sum
function is used to sum the elements of the list of pairs. -
The
mod
function is used to take the remainder of the sum of the list of pairs when divided by 10. -
The
(0 ==)
function is used to compare the remainder of the sum of the list of pairs when divided by 10 to 0. If the remainder is equal to 0, the function returnsTrue
; otherwise, it returnsFalse
.
The second implementation of the Luhn algorithm is similar to the first, but it uses a different approach. It uses the transpose
and chunksOf
functions from the Data.List
and Data.List.Split
libraries to separate the digits of the credit card number into odd and even positions. It then applies the Luhn algorithm to each set of digits separately and sums the results.
Source code in the haskell programming language
import Data.Char (digitToInt)
luhn = (0 ==) . (`mod` 10) . sum . map (uncurry (+) . (`divMod` 10)) .
zipWith (*) (cycle [1,2]) . map digitToInt . reverse
map luhn ["49927398716", "49927398717", "1234567812345678", "1234567812345670"]
[True,False,False,True]
import Data.Char (digitToInt)
import Data.List (transpose)
import Data.List.Split (chunksOf)
luhn :: String -> Bool
luhn x = 0 == rem (s1 + s2) 10
where
stringInts = fmap digitToInt
[odds, evens] =
(transpose . chunksOf 2)
(stringInts $ reverse x)
s1 = sum odds
s2 = sum $ sum . stringInts . show . (2 *) <$> evens
main :: IO ()
main =
mapM_
(print . ((,) <*> luhn))
[ "49927398716",
"49927398717",
"1234567812345678",
"1234567812345670"
]
You may also check:How to resolve the algorithm Polyspiral step by step in the Lua programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the R programming language
You may also check:How to resolve the algorithm Date format step by step in the Emacs Lisp programming language
You may also check:How to resolve the algorithm Increment a numerical string step by step in the V (Vlang) programming language
You may also check:How to resolve the algorithm First perfect square in base n with n unique digits step by step in the Julia programming language