Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amalloy/1180384 to your computer and use it in GitHub Desktop.
Save amalloy/1180384 to your computer and use it in GitHub Desktop.
;; amalloy's solution to Levenshtein Distance
;; https://4clojure.com/problem/101
(letfn [(iters [n f start]
(take n (map second
(iterate f start))))]
(fn [s t]
(let [m (inc (count s)), n (inc (count t))
first-row (vec (range m))
matrix (iters n (fn [[j row]]
[(inc j)
(vec (iters m (fn [[i col]]
[(inc i)
(if (= (nth s i)
(nth t j))
(get row i)
(inc (min (get row i)
(get row (inc i))
col)))])
[0 (inc j)]))])
[0 first-row])]
(last (last matrix)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment