Off topic: I am reading Paul Graham's ANSI Common Lisp and I have a little question: why does he uses a lot (at least until chapter 4) "and" clauses when I think he should use the "when" macro? For example, in this code snip from figure 4.1:
;; This is a binary search algorithm as you may guess. (defun bin-search (obj vec) (let ((len (length vec))) (and (not (zerop len)) ;Here is the and clause that I may change for ;a when (finder obj vec 0 (- len 1)))))
(defun finder (obj vec start end) ...)
I've read that at least three times I think and it seems that there will be more. I know that the and clause gives the desired behavior of returning the last value that satisfies it or the first one that doesn't, but I would have used a when for some of the cases I've read (it is more readable for me, I think). Is he using the and clause for some reason that eludes me or is it just a personal taste?
In many cases I think it's just personal taste, i.e., a style issue like the one I mentioned above.
But maybe not. If you consider the published code as a snapshot of a real application, some method to this madness emerges (maybe). In particular, consider the code over time. Perhaps he started out with
(and (not (zerop len)) (other code) (finder obj vec 0 (- len 1)))
and then deleted the second clause. Maybe he didn't want to change it. Maybe he thought he might, at some later date, have to add some more code in the middle. Once you shift back and forth between
(when (foo) (baz))
and
(when (and (foo) (bar)) (baz))
enough times, you start to want to skip the middle man and just go for
(and (foo) (baz))
every time, in case you have to add the middle (bar) later.
Or so it seems to me, anyway. :)
I haven't really thought about it all that much, but I might use the following rule of thumb: Start out with
(when (foo) (baz))
The first time you have to change it to
(when (and (foo) (bar)) (baz))
change it to
(and (foo) (bar) (baz))
instead and leave it that way forever after. If it has changed once, it'll probably change again. :)
-- Larry
quiz mailing list quiz@common-lisp.net http://common-lisp.net/cgi-bin/mailman/listinfo/quiz
Outstanding. Thanks for the answer, Larry. I was starting to think that was the answer, but you stated it very clear.