# Re: COPYALL, COPYLIST, et alia

• Subject: Re: COPYALL, COPYLIST, et alia
• From: Guy.Steele at CMU-10A
• Date: Tue ,20 Jan 81 17:59:00 EDT

```Another, more general copy routine would take a predicate and
a thing, and copy the thing recursively iff the predicate was
true of the object.  (Entities should probably be copied by
sending a :COPY message.)  Thus:
xxxx
Let me go further.  Let the predicate be a pseudo-predicate
determining when *not* to copy.  Moreover, if the predicate
returns non-() then the value of the predicate is returned
as the value of this new function (call it COP, by analogy
with ASS and MEM and DEL).  This gives the "predicate" an
opportunity to do specialized copying itself.  If the object
is (), then there is no good way to say not to copy it
(because to do that you would have to return () -- but that
means to copy the argument!); but copying () never hurts
anyway, right?  SO...
(DEFUN COP (PRED X)
(OR (FUNCALL PRED X)
(TYPE-DISPATCH X
((FIXNUM) X)
((STRING) (COPY-STRING X))
((LIST) (CONS (COP PRED (CAR X)) (COP PRED (CDR X))))
...)))
Then:
(COPYTREE X) = (COP #'(LAMBDA (Z) (AND (ATOM Z) Z)) X)
(COPYLIST X) = (COP #'(LAMBDA (Z) (IF (PAIRP Z)
(CONS (CAR Z) (COPYLIST (CDR Z)))
Z))
X)
;kind of a sledgehammer approach
(COPYKLUDGE X) = (COP #'(LAMBDA (Z) (COND ((STRINGP Z) (STRING-UPCASE Z))
((ATOM Z) Z)))
X)
;this copies an entire tree and also converts strings to upper case

Actually, I think this is all bletcherous, but maybe someone
can improve this half-baked idea.
--Guy
```