views:

101

answers:

5

How can I pass a variable by reference in scheme?

An example of the functionality I want:

(define foo
  (lambda (&x)
    (set! x 5)))

(define y 2)

(foo y)

(display y) ;outputs: 5

Also, is there a way to return by reference?

+8  A: 

See http://community.schemewiki.org/?scheme-faq-language question "Is there a way to emulate call-by-reference?".

In general I think that fights against scheme's functional nature so probably there is a better way to structure the program to make it more scheme-like.

Jari
Cool, thanks. Good to know, but it looks like I should probably figure out a better way to do what I'm trying to do :)
Cam
That's a good reference on emulating a "by-reference" argument using boxes -- and note that the result of using `box`, `unbox` and `set-box!` is very close to using an explicit pointer in C. Also, it's rare to use such a thing, but it *can* be useful on some occasions -- Scheme encourages functional programming and discourages mutation, but it doesn't have some fundamental objection to doing so. (But in most cases it is likely that newbie-code doing this needs some rethinking.)
Eli Barzilay
+2  A: 

Like Jari said, usually you want to avoid passing by reference in Scheme as it suggests that you're abusing side effects.

If you want to, though, you can enclose anything you want to pass by reference in a cons box.

(cons 5 (void))

will produce a box containing 5. If you pass this box to a procedure that changes the 5 to a 6, your original box will also contain a 6. Of course, you have to remember to cons and car when appropriate.

Chez Scheme (and possibly other implementations) has a procedure called box (and its companions box? and unbox) specifically for this boxing/unboxing nonsense: http://www.scheme.com/csug8/objects.html#./objects:s43

erjiang
A: 

You probably have use too much of C, PHP or whatever. In scheme you don't want to do stuff like pass-by-*. Understand first what scope mean and how the different implementation behave (in particular try to figure out what is the difference between LISP and Scheme).

By essence a purely functional programming language do not have side effect. Consequently it mean that pass-by-ref is not a functional concept.

mathk
+2  A: 

lambda!

(define (foo getx setx)
  (setx (+ (getx) 5)))

(define y 2)
(display y)(newline)

(foo
 (lambda () y)
 (lambda (val) (set! y val)))

(display y)(newline)
grettke
+2  A: 

Jari is right it is somewhat unscheme-like to pass by reference, at least with variables. However the behavior you want is used, and often encouraged, all the time in a more scheme like way by using closures. Pages 181 and 182(google books) in the seasoned scheme do a better job then I can of explaining it.

Here is a reference that gives a macro that allows you to use a c like syntax to 'pass by reference.' Olegs site is a gold mine for interesting reads so make sure to book mark it if you have not already.

http://okmij.org/ftp/Scheme/pointer-as-closure.txt

Davorak