Created
March 30, 2012 19:27
-
-
Save chribben/2254267 to your computer and use it in GitHub Desktop.
Luhn implementation F#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let luhn (inp:string) = | |
let algo (s:string) = | |
Regex.Replace(s, "[^0-9]", "").ToCharArray() | |
|> Array.rev | |
|> Seq.map(int << string) | |
|> Seq.fold (fun (fldr,weight) num -> fldr + (num * weight) / 10 + (num * weight) % 10, weight % 2 + 1) (0,2) | |
|> fst | |
match inp with | |
| "" -> 0 | |
| _ -> 10 - (algo inp) % 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment