How to resolve the algorithm Luhn test of credit card numbers step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

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:

  1. The map function is used to apply the luhn function to each element of the list of credit card numbers. The luhn function takes a string as input and returns a Bool indicating whether the string is a valid credit card number.

  2. The luhn function takes a string as input and converts it to a list of integers using the digitToInt function from the Data.Char library.

  3. The list of integers is then reversed using the reverse function.

  4. 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 the cycle function from the Data.List library.

  5. The map function is used to apply the uncurry function to each element of the list of pairs. The uncurry function takes a function of two arguments and returns a function of one argument that applies the function of two arguments to its argument.

  6. The (+) function is a function that takes two numbers and returns their sum.

  7. 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.

  8. The map function is used to apply the divMod function to each element of the list of pairs.

  9. The sum function is used to sum the elements of the list of pairs.

  10. The mod function is used to take the remainder of the sum of the list of pairs when divided by 10.

  11. 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 returns True; otherwise, it returns False.

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