Hey!
I am pretty new to lisp and have one big problem that has
been bothering me really hard recently:
Is there a way to have calls by reference when passing
arguments to lisp functions? Let me demonstrate what I want
to have with this short code fragment:
(defun afun (x)
(setf x 333))
(defun bfun ()
(let ((z 7))
(print z) ; is 7 here, that is fine with me
(afun z)
(print z) ; I want b to be 10 now!!
; but it is 7!!
)
)
So basically I'd like to know if there is a way to extend a
variables scope not only to the form it is created within
but also to forms or functions called from within that
form... does this sound confusing? Well I think the example
above should be pretty self explaining what I am looking
for.
Is there anybody who can help me? I'd be really grateful!
Thanx
Chb
(defun cfun ()
(let ((z 5))
(bfun)
(print z) ; this outputs 5, that's ok
)
)
* Sent from AltaVista http://www.altavista.com Where you can also find related Web Pages, Images, Audios, Videos, News, and Shopping. Smart is Beautiful
In article <·················@usw-ex0110-075.remarq.com>,
chb <················@gmx.net.invalid> wrote:
>Hey!
>I am pretty new to lisp and have one big problem that has
>been bothering me really hard recently:
>
>Is there a way to have calls by reference when passing
>arguments to lisp functions? Let me demonstrate what I want
>to have with this short code fragment:
No. Macros are used when this is needed.
--
Barry Margolin, ······@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
chb <················@gmx.net.invalid> wrote in message
······················@usw-ex0110-075.remarq.com...
| Hey!
| I am pretty new to lisp and have one big problem that has
| been bothering me really hard recently:
|
| Is there a way to have calls by reference when passing
| arguments to lisp functions? Let me demonstrate what I want
| to have with this short code fragment:
|
| (defun afun (x)
| (setf x 333))
|
| (defun bfun ()
| (let ((z 7))
| (print z) ; is 7 here, that is fine with me
| (afun z)
| (print z) ; I want b to be 10 now!!
| ; but it is 7!!
| )
| )
|
| So basically I'd like to know if there is a way to extend a
| variables scope not only to the form it is created within
| but also to forms or functions called from within that
| form... does this sound confusing? Well I think the example
| above should be pretty self explaining what I am looking
| for.
|
| Is there anybody who can help me? I'd be really grateful!
| Thanx
|
| Chb
| (defun cfun ()
| (let ((z 5))
| (bfun)
| (print z) ; this outputs 5, that's ok
| )
| )
|
|
|
|
| * Sent from AltaVista http://www.altavista.com Where you can also find related Web
Pages, Images, Audios, Videos, News, and Shopping. Smart is Beautiful
Try:
(defun bfun()
(let ((z 7))
(print (afun z))))
or:
(defun bfun()
(let ((z 7))
(setf z (afun z))
(print z)))
or:
(defmacro afun(x)
`(setf ,x 333))
(defun bfun ()
(original code will now work))
Coby
chb <················@gmx.net.invalid> writes:
> Is there a way to have calls by reference when passing
> arguments to lisp functions? Let me demonstrate what I want
> to have with this short code fragment:
Take a look at recent (last 3 months) postings in c.l.l on this topic
at deja.com. The short summary is that you normally don't do call by
reference in CL, but use (multiple) return values and/or macros
instead.
Here's a snippet of code from a CRC32 generation routine that shows
both approaches together:
;; This function returns an updated CRC32
(defun update-crc32-byte (crc byte)
(declare (type (unsigned-byte 8) byte)
(type (unsigned-byte 32) crc))
(logxor (aref *crc32-table*
(coerce (logand (logxor crc byte) #xFF) 'fixnum))
(ash crc -8)))
;; This is the in-place modify macro
(define-modify-macro updatef-crc32 (byte) update-crc32-byte)
;; Now you can do the following
(loop with crc = +crc32-initial-value+
for byte across image
do
(updatef-crc32 crc byte)
finally (return crc))
;; OTOH you could just as well do
(loop for byte across image
for crc = (update-crc32-byte +crc32-initial-value+ byte)
then (update-crc32-byte crc byte)
finally (return crc))
You might want to take a look at code that's written in CL, to see how
this is normally tackled in CL. You might also want to take a look at
the parts of the HyperSpec[1] that deal with generalized reference,
multiple-values and other stuff. Any good introductory book to CL
should also tackle those issues.
Regs, Pierre.
Footnotes:
[1] The HyperSpec is a translation of the text of the ANSI CL standard to
HTML done by Kent M. Pitman, then at Harlequin. It can be accessed at
http://www.xanalys.com/software_tools/reference/hyperspec.html for the
on-line copy and information on downloading a copy for use off-line.
--
Pierre Mai <····@acm.org> PGP and GPG keys at your nearest Keyserver
"One smaller motivation which, in part, stems from altruism is Microsoft-
bashing." [Microsoft memo, see http://www.opensource.org/halloween1.html]