Subject: Re: Code review: CL for SNA (Social Network Analysis)
From: rpw3@rpw3.org (Rob Warnock)
Date: Wed, 21 Dec 2005 04:47:37 -0600
Newsgroups: comp.lang.lisp
Message-ID: <tbudncQrY7VUrzTeRVn-uw@speakeasy.net>
Christophe Rhodes  <csr21@cam.ac.uk> wrote:
+---------------
| rpw3@rpw3.org (Rob Warnock) writes:
| > (defun prefix-match-p (prefix line)
| >   (not (mismatch prefix line :end2 (length prefix) :test #'char-equal)))
| 
| I think you need
| (defun prefix-match-p (prefix line)
|   (and (>= (length line) (length prefix))
|        (not (mismatch prefix line :end2 (length prefix) :test #'char-equal))))
| otherwise you run the risk of signalling an error, which isn't what
| you want, when the argument to END2 is larger than the length of the line.
+---------------

Oops! Correct, good point.

+---------------
| That can also be written
|   (defun prefix-match-p (prefix line)
|     (let ((mismatch (mismatch prefix line :test #'char-equal)))
|       (or (null mismatch) (= mismatch (length prefix)))))
+---------------

O.k., but to my taste this is slightly simpler:

    (defun prefix-match-p (prefix line)
      (not (mismatch prefix line :end2 (min (length prefix) (length line))
				 :test #'char-equal)))


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607