[plt-scheme] vectors by value

From: John T. Murphy (jtm at perfectknowledgedb.com)
Date: Fri Feb 7 21:26:49 EST 2003

This is probably a newbie question...

With the following executed in the definitions window:

(define mylist '(hi there))
(define changemylist 
  (lambda (x)
    (set! x (append x '(this is new)))
    (display x)(newline)
    x))

(define myvector (list->vector '(hi there)))
(define changemyvector
  (lambda (x)
    (vector-set! x 1 'yall)
    (display x)(newline)
    x))

I find the following in the interactions window:

Welcome to DrScheme, version 202.
Language: Pretty Big (includes MrEd and Advanced).
> mylist
(hi there)
> (changemylist mylist)
(hi there this is new)
(hi there this is new)
> mylist
(hi there)
> myvector
#2(hi there)
> (changemyvector myvector)
#(hi yall)
#2(hi yall)
> myvector
#2(hi yall)
> 

So, really I have two questions. First, is this indicating that for
regular lists the argument is passed 'by value' and for vectors it's
passed 'by reference'? I know these terms are pulled from other
platforms, but what I mean, of course, is that resetting the value of
the local variable within the function in the first example doesn't
change the original list, but in the second it does change the original
vector.

Second, how do I avoid this? I'd like to have a function that receives a
list with vectors as some of its elements, and returns a variation of
this without changing the original.

Will happily review documentation if someone points me to it, but so far
I haven't been able to find anything relevant on my own. 

Thanks,
John

John T. Murphy
University of Arizona
Department of Anthropology
jtmurphy at email.arizona.edu





Posted on the users mailing list.