Skip to content

Instantly share code, notes, and snippets.

@noahmorrison
Created November 22, 2014 01:15
Show Gist options
  • Save noahmorrison/1fce5b0b98ff69c44270 to your computer and use it in GitHub Desktop.
Save noahmorrison/1fce5b0b98ff69c44270 to your computer and use it in GitHub Desktop.
Levenshtein distance in lisp (Recursive/slow)
(defun leven-distance (string1 string2)
(if (= (length string1) 0)
(length string2)
(if (= (length string2) 0)
(length string1)
(min (+ (leven-distance (subseq string1 0 (- (length string1) 1))
string2)
1)
(+ (leven-distance string1
(subseq string2 0 (- (length string2) 1)))
1)
(+ (leven-distance (subseq string1 0 (- (length string1) 1))
(subseq string2 0 (- (length string2) 1)))
(if (equal (subseq string1 (- (length string1) 1))
(subseq string2 (- (length string2) 1)))
0 1))))))
(defun get-string (prompt)
(format T prompt)
(read-line *query-io*))
(defun main ()
(format T "The diff is: ~a"
(leven-distance
(get-string "string1: ")
(get-string "string2: ")))
(quit))
(main)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment