Is conditional expression in Conad Barsky function lisp redundant?

This question is related to the code in Chapter 6 of Conrad Barsky, Land of Lisp .

The code is as follows

 (defun tweak-text (lst caps lit) (when lst (let ((item (car lst)) (rest (cdr lst))) (cond ((eq item #\space) (cons item (tweak-text rest caps lit))) ((member item '(#\! #\? #\.)) (cons item (tweak-text rest t lit))) ((eq item #\") (tweak-text rest caps (not lit))) (lit (cons item (tweak-text rest nil lit))) ((or caps lit) (cons (char-upcase item) (tweak-text rest nil lit))) (t (cons (char-downcase item) (tweak-text rest nil nil))))))) 

Now look at the part (lit ..) and the material below it. ((or caps nil) ..) , so my question is as follows

  • If lit always true, it will be evaluated in the previous expression specified
  • If this is not the case, the last expression will always be evaluated as (or caps false) => (or caps false) , which is practically useless?

Therefore, the last expression should not be (caps (cons (char ...)) ?

This book has been read by thousands, so I must be wrong about something, and I'm not John Bell.

+7
lisp common-lisp
source share
2 answers

Yes, a simpler expression is equivalent. This is mentioned on page 97 of errata http://landoflisp.com/errata.html

+8
source share

One problem is the use of recursion, which limits the length of lists that the function can process.

 (defun tweak-text (list &aux (caps t) (lit nil)) (mapcon (lambda (c) (case c (#\space (list c)) ((#\! #\? #\.) (setf caps t) (list c)) (#\" (setf lit (not lit)) ()) (otherwise (cond (lit (setf caps nil) (list c)) (caps (setf caps nil) (list (char-upcase c))) (t (setf caps nil lit nil) (list (char-downcase c))))))) list)) 
+2
source share

All Articles