Writing a specialised string comparison function


One of the less obvious improvements in uLisp 4.6 is that the string comparison functions now return an integer specifying the first character mismatch, rather than just t, if the comparison succeeds. This now matches Common Lisp’s policy of, where possible, having predicates return a useful non-nil value rather than just t.

One application of this is when writing your own specialised string search function. For example, the following function string=* behaves like string=, except that a * at the end of one string matches zero or more characters in the other string:

(defun string=* (string1 string2)
  (let ((nomatch (string/= string1 string2)))
     ((not nomatch) t)
     ((and (> (length string1) nomatch) (eq (char string1 nomatch) #\*)) t)
     ((and (> (length string2) nomatch) (eq (char string2 nomatch) #\*)) t)
     (t nil))))

For example:

> (string=* "fred" "fred")

> (string=* "fred" "fredabc")

> (string=* "fred*" "fredabc")

I’d be interested in suggestions of any other examples.