From ...
Path: archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-01!supernews.com!newsfeed.online.be!skynet.be!skynet.be!ossa.telenet-ops.be!nmaster.kpnqwest.net!nreader1.kpnqwest.net.POSTED!not-for-mail
Newsgroups: comp.lang.lisp
Subject: Re: quest for pass-by-reference semantics in CL
References:
Mail-Copies-To: never
From: Erik Naggum
Message-ID: <3229277251241473@naggum.net>
Organization: Naggum Software, Oslo, Norway
Lines: 43
User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Date: Wed, 01 May 2002 21:27:31 GMT
X-Complaints-To: newsmaster@KPNQwest.no
X-Trace: nreader1.kpnqwest.net 1020288451 193.71.199.50 (Wed, 01 May 2002 23:27:31 MET DST)
NNTP-Posting-Date: Wed, 01 May 2002 23:27:31 MET DST
Xref: archiver1.google.com comp.lang.lisp:32584
* Mark Coletti
| But I _want_ FROB to modify its argument; i.e., I'd like
| pass-by-reference semantics.
You want wrong. In Common Lisp, we return values and the caller modifies
whatever needs to be modified. Not all languages are variations on C++
such that you can basically continue to work in C++. E.g., Common Lisp
can return multiple values and programmers use this instead of pass-by-
reference because returning more than one value is better than passing in
pointers to and clobbering somebody else's storage space.
However, if you are so dead set on doing this that you cannot be turned
away from misguded application of C++ semantics, here is one way to
accomplish this:
(defun frob (setter)
(funcall setter 42))
(let ((a 12))
(print a)
(frob (lambda (value) (setf a value)))
(print a))
Note how the variable to be modified is captured and that you cannot
screw around with any pointers.
You can make this more convenient with this macro
(defmacro setter (place)
(let ((value (make-symbol "value")))
`(lambda (,value)
(setf ,place ,value))))
This technique may be used to accomplish half the pass-by-name semantics
from Algol, but there is no way to pass in a variable that will appear as
just a variable without undue amounts of macrology.
But I think you should simply learn to write Common Lisp in Common Lisp.
--
In a fight against something, the fight has value, victory has none.
In a fight for something, the fight is a loss, victory merely relief.
70 percent of American adults do not understand the scientific process.