Skip to content

Instantly share code, notes, and snippets.

@littleli
Last active June 6, 2020 23:42
Show Gist options
  • Select an option

  • Save littleli/5010d2cfea897432d7a26c72f795d782 to your computer and use it in GitHub Desktop.

Select an option

Save littleli/5010d2cfea897432d7a26c72f795d782 to your computer and use it in GitHub Desktop.
String distance function
(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