Skip to content

Instantly share code, notes, and snippets.

@fbmnds
Created February 23, 2013 20:10
Show Gist options
  • Select an option

  • Save fbmnds/5021142 to your computer and use it in GitHub Desktop.

Select an option

Save fbmnds/5021142 to your computer and use it in GitHub Desktop.
Levenshtein Distance - 4clojure #101
(fn f [a b]
(letfn [(clear-string [a]
(cond (string? a) a
(vector? a) (clojure.string/join (map str a))
:else (clojure.string/join
(filter #(not (= % \:))
(clojure.string/join (map str a))))))
(make-ls-d []
(with-local-vars
[ls-d (memoize
(fn f [a b]
(cond (empty? a) (count b)
(empty? b) (count a)
:else (let [cost (if (= (last a) (last b)) 0 1)
a-1 (clojure.string/join (butlast a))
b-1 (clojure.string/join (butlast b))]
(min
(inc (ls-d a-1 b))
(inc (ls-d a b-1))
(+ (ls-d a-1 b-1) cost))))))]
(.bindRoot ls-d @ls-d)
@ls-d))]
(let [ls-d (make-ls-d)]
(ls-d (clear-string a) (clear-string b)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment