Levenshtein distance: Difference between revisions

→‎{{header|Common Lisp}}: Local functions should be declared using `labels` or `flet`. `defun` always binds the function to a name in the package, an unwanted side effect here.
(→‎{{header|Rust}}: more concise and efficient initialization of 'matrix')
(→‎{{header|Common Lisp}}: Local functions should be declared using `labels` or `flet`. `defun` always binds the function to a name in the package, an unwanted side effect here.)
Line 1,247:
<lang lisp>(defun levenshtein (a b)
(let* ((la (length a))
(lb (length b))
(rec (make-array (list (1+ la) (1+ lb)) :initial-element nil)))
(labels ((leven la(x lb))y)
 
(defun leven (x y) (cond
(cond(zerop x) y)
((zerop xy) yx)
((aref rec x y) (aref rec x y))
((zerop y) x)
((aref rec x y) (t (setf (aref rec x y))
(+ (if (char= (char a (- la x)) (char b (- lb y))) 0 1)
(t (setf (aref rec x y)
(+ (if (char= (char a (- la x)) (charmin b(leven (1- lb y))x) 0 1y)
(min (leven x (1- x) y))
(leven (1- x) (1- y)))))))))
(leven (1- x) (1-leven la y))))lb))))
(leven la lb)))
 
(print (levenshtein "rosettacode" "raisethysword"))</lang>
Anonymous user