Last active
June 6, 2020 23:42
-
-
Save littleli/5010d2cfea897432d7a26c72f795d782 to your computer and use it in GitHub Desktop.
String distance function
This file contains hidden or 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
| (ns strdist | |
| (:require [clojure.core.match :refer [match]] | |
| [clojure.string :as str])) | |
| (defn -distance | |
| [s1 s2] | |
| (match [s1 s2] | |
| [nil _] (.length s2) | |
| [_ nil] (.length s1) | |
| [([l & ls] :seq) ([r & rs] :seq)] (min (+ (if (= l r) 0 1) (-distance ls rs)) | |
| (+ 1 (-distance ls s2)) | |
| (+ 1 (-distance s1 rs))) | |
| :else 0)) | |
| (defn distance | |
| [s1 s2] | |
| (-distance (seq s1) (seq s2))) | |
| (comment | |
| (distance "abc" "dec") | |
| (distance "aaa" "bbb") | |
| ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment