Created
July 31, 2023 08:02
-
-
Save MattMS/074274e3330e954063df6120f08cd180 to your computer and use it in GitHub Desktop.
Get Levenshtein distance
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
// 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