(longest-common-prefix x y) returns the longest list,
(longest-common-prefix x y) → *
See also longest-common-prefix-list, which extends this function to a list of lists.
Function:
(defun longest-common-prefix (x y) (declare (xargs :guard t)) (let ((__function__ 'longest-common-prefix)) (declare (ignorable __function__)) (cond ((or (atom x) (atom y)) nil) ((equal (car x) (car y)) (cons (car x) (longest-common-prefix (cdr x) (cdr y)))) (t nil))))
Theorem:
(defthm true-listp-of-longest-common-prefix (true-listp (longest-common-prefix x y)) :rule-classes :type-prescription)
Theorem:
(defthm string-listp-of-longest-common-prefix (implies (and (string-listp x) (string-listp y)) (string-listp (longest-common-prefix x y))))
Theorem:
(defthm longest-common-prefix-of-list-fix-left (equal (longest-common-prefix (list-fix x) y) (longest-common-prefix x y)))
Theorem:
(defthm longest-common-prefix-of-list-fix-right (equal (longest-common-prefix x (list-fix y)) (longest-common-prefix x y)))
Theorem:
(defthm prefixp-of-longest-common-prefix-left (prefixp (longest-common-prefix x y) x))
Theorem:
(defthm prefixp-of-longest-common-prefix-right (prefixp (longest-common-prefix x y) y))
Theorem:
(defthm longest-common-prefix-preserves-prefixp (implies (prefixp p a) (prefixp (longest-common-prefix p b) a)))