Skip to content

Instantly share code, notes, and snippets.

@MattMS
Created July 31, 2023 08:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MattMS/074274e3330e954063df6120f08cd180 to your computer and use it in GitHub Desktop.
Save MattMS/074274e3330e954063df6120f08cd180 to your computer and use it in GitHub Desktop.
Get Levenshtein distance
// Formula from https://en.wikipedia.org/wiki/Levenshtein_distance
let lev (a : string) (b : string) =
let rec loop (a : char list) (b : char list) =
match a, b with
| a, [] -> a.Length
| [], b -> b.Length
| (aHead :: aTail), (bHead :: bTail) when aHead = bHead -> loop aTail bTail
| (_ :: aTail), (_ :: bTail) -> 1 + List.min [loop aTail b ; loop a bTail ; loop aTail bTail]
loop (List.ofSeq a) (List.ofSeq b)
match List.ofArray fsi.CommandLineArgs with
| []
| [_] -> printfn "Please provide 2 words"
| [_ ; a] -> lev a "" |> printfn "%i"
| [_ ; a ; b] -> lev a b |> printfn "%i"
| args -> lev args[1] args[2] |> printfn "%i"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment