[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Writing Destructive Functions

  I understand what you're point that most "good" lisp functions return a
result, but there are a few examples where it is a waste to constantly
have to write (setf my-var (do-something my-var))
A great example is the "(incf counter)" type of function, where you always
want to increment (or decrement) the counter in place.

The second important area is in string manipulation, where copying and
creating large strings entails a significant amount of overhead.  When you
call a string function, it seems that in MCL ( and other LISPs?) you
always get a copy of the string, a real waste in many applications.

The other time this becomes important is when I need to alter the head of
several lists passed to my functions.  If I am changing (i.e. adding
something to) the head of a single list, I can just return that list... no
problem, but If I have to do this with several lists that are passed as
parameters I run into a lot of problems.

The LISP way out seems to be using (multiple-value-bind) but this is a
very clumsy macro that makes it awkward to change mulitple variables
within the block, i.e.

(let ((x 0) (y 0))
;; do something with x & y
  (multiple-value-bind (m n)
    (truncate 3.4565)
   (setf x m)
  (setf y n))
;; do something else with x & y

You get the idea.  I could use macros but, having been burned many times
in the past (especially in C) I would much prefer to write real functions.