Path: utzoo!mnetor!uunet!husc6!rutgers!ames!sdcsvax!ucsdhub!hp-sdd!hplabs!hpcea!hpfcdc!hpfclp!diamant From: diamant@hpfclp.HP.COM (John Diamant) Newsgroups: comp.lang.lisp Subject: Re: Correctness (was Re: Common Lisp lacks portability) Message-ID: <6950004@hpfclp.HP.COM> Date: 19 Dec 87 02:31:43 GMT References: <859@rocky.STANFORD.EDU> Organization: HP, Fort Collins, CO Lines: 40 > (defun silly (a b) > (declare (integer a b)) > (loop i from a to b do (print i))) > > Silly does not handle illegal input - its definition promises that > its input is well-formed. Safe-silly handles illegal input. I agree with your example about safe-silly versus silly, but I'd like to point out that safe-silly needn't be as ugly as you wrote in your example. Below your example, see a complete, correct implementation of safe-silly that doesn't require that bizarre (though quite correct) let. Note, that I am not attempting to criticize your understanding of Common Lisp -- you clearly understand it. I only wish to point out the the safe program can be very clean (more so than in your example). > > (defun safe-silly (a b) > appropriate action if it isn't. Note that competent CL programmers > know how to return a value from safe-silly without executing the > remainder of this function.> > (let ((a a) (b b)) > (declare (integer a b)) > (loop i from a to b do (print i)))) (defun safe-silly (a b) (check-type a integer) (check-type b integer) (loop i from (the integer a) to (the integer b) do (print i))) Note that check-type is part of CL and provides an optional string argument to provide your own error message, and that with the use of the special form "the," no extra storage is needed. Clearly, in this example, the efficiency isn't that significant, but in heavy calculations, it may well make a big difference. In a much larger main body, the use of the let as above may well be worth it (if a and b appear several times). John Diamant UUCP: {hplabs,hpfcla}!hpfclp!diamant Hewlett Packard Co. ARPA Internet: diamant%hpfclp@hplabs.HP.COM Fort Collins, CO