Skip to content

Instantly share code, notes, and snippets.

@chribben
Created March 30, 2012 19:27
Show Gist options
  • Save chribben/2254267 to your computer and use it in GitHub Desktop.
Save chribben/2254267 to your computer and use it in GitHub Desktop.
Luhn implementation F#
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