Sha256: 1ad57537cf3eedb5dd2167b9be051f28dbf8360be56b6052a0bb01af7769b29c

Contents?: true

Size: 889 Bytes

Versions: 167

Compression:

Stored size: 889 Bytes

Contents

module Luhn (checkDigit, addends, checksum, isValid, create) where

revDigits :: Integral a => a -> [a]
revDigits n = rem10 : digits
  where (quot10, rem10) = n `quotRem` 10
        digits | quot10 == 0 = []
               | otherwise   = revDigits quot10

luhnDouble :: Integral a => a -> a
luhnDouble n | n < 5     = n * 2
             | otherwise = n * 2 - 9

luhnDigits :: Integral a => a -> [a]
luhnDigits = zipWith ($) (cycle [id, luhnDouble]) . revDigits

checkDigit :: Integral a => a -> a
checkDigit = head . revDigits

addends :: Integral a => a -> [a]
addends = reverse . luhnDigits

checksum :: Integral a => a -> a
checksum = (`rem` 10) . sum . luhnDigits

isValid :: Integral a => a -> Bool
isValid = (0 ==) . checksum

create :: Integral a => a -> a
create n | chk == 0  = n10
         | otherwise = n10 + (10 - chk)
  where n10 = n * 10
        chk = checksum n10 `rem` 10

Version data entries

167 entries across 167 versions & 1 rubygems

Version Path
trackler-2.1.0.11 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.10 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.9 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.8 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.7 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.6 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.5 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.4 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.3 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.2 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.1 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.1.0.0 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.55 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.54 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.53 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.52 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.51 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.50 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.49 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs
trackler-2.0.8.48 tracks/haskell/exercises/luhn/examples/success-standard/src/Luhn.hs