Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!spool.mu.edu!snorkelwacker.mit.edu!ai-lab!zurich.ai.mit.edu!jinx From: jinx@zurich.ai.mit.edu (Guillermo J. Rozas) Newsgroups: comp.lang.scheme Subject: Re: How to pass parameter by reference in scheme? Message-ID: Date: 2 Mar 91 15:17:50 GMT References: <1519@acf5.NYU.EDU> Sender: news@ai.mit.edu Reply-To: jinx@zurich.ai.mit.edu Organization: M.I.T. Artificial Intelligence Lab. Lines: 62 In-Reply-To: zql4991@acf5.NYU.EDU's message of 2 Mar 91 05:43:41 GMT In article <1519@acf5.NYU.EDU> zql4991@acf5.NYU.EDU (Zhiqing Liu) writes: Path: ai-lab!snorkelwacker.mit.edu!usc!zaphod.mps.ohio-state.edu!wuarchive!rice!uupsi!cmcl2!acf5!zql4991 From: zql4991@acf5.NYU.EDU (Zhiqing Liu) Newsgroups: comp.lang.scheme Date: 2 Mar 91 05:43:41 GMT Organization: New York University Lines: 3 The Subject has told what I want. Thank you. --Zhiqing Liu (liuz@cs.nyu.edu) It depends on exactly what you mean. If you want to pass an object that can then be mutated by the callee and the mutation visible to the caller, you can just pass it normally, since all objects are passed by reference in Scheme. Of course, not all objects are mutable. If what you mean is that you want to pass a VARIABLE so that the callee can mutate the variable's value for the caller, you are out of luck. Scheme always passes arguments by value (but the objects passed are themselves references). On the other hand, if you really need to do this (see below), you can pass a procedure of one argument (the new value) that when invoked will set the original parameter. For example: (define (write-line obj) ; Lisp's PRINT (newline) (write obj) (write-char #\space)) (define (test) (let ((my-local-x 23)) (write-line `(my-local-x ,my-local-x)) (effect! (lambda () my-local-x) (lambda (val) (set! my-local-x val))) (write-line `(my-local-x ,my-local-x)))) (define (effect! fetch store!) (let ((val (fetch))) (store! (* val val)))) This is not quite passing a variable by reference. It is closer to Algol 60's call-by-name, and mimics some of its implementations. If you use this pervasively, you will notice subtle differences with passing parameters by reference (e.g. Jensen's device). An alternative is to put your values in data structures (cells, boxes, pairs) and pass those around. Since you can't get the syntax to work right anyway, this may be better and will really mimic pass-by-reference. The other question is why you would want to pass a variable by reference. Although this is a common way to do things in Pascal and C, it is typically used mostly for aggregates (structures, records, arrays, strings), and you get the same effect in Scheme by passing them (the references) by value. I suggest that you rethink your task in a way that does not require you to pass references to variables. Often the resulting programs are clearer and easier to understand. Effects should not be used spuriously.