Home > Archive > Scheme > November 2004 > pass addresses or values? Help.
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
pass addresses or values? Help.
|
|
| Phoenix77 2004-11-10, 4:00 pm |
| Hello,
I writen 2 functions and was puzzled...
;Chez Scheme
> (define l '(a b c))
> (define s
(lambda (x)
(set-car! x 3)))
> (s l)
> l
(3 b c)
; I found it different from C programming language
; why not return '(a b c) ?
> (define x '(3 b c))
> (define s2
(lambda (x)
(set! x '(x y z))))
> (s2 l)
> l
(3 b c)
; I am puzzled .
; According to the last function,
; I want to get the value '(x y z)
; but failed .
; Any one can tell me why? Thanks.
| |
| Phoenix77 2004-11-10, 4:00 pm |
| And I test following s-expresses:
> l
(3 b c)
> (eq? (car l) (car l))
#t
> (define l2 l)
> (eq? (car l) (car l2))
#t
> (set-car! l2 'a)
> l
(a b c) ; l
> l2
(a b c) ;
> (set-cdr! l2 '(x y))
> l2
(a x y) ;
> l
(a x y) ;
>
; So I get a conclusion that the addresses of l and l2 are identical.
; Are they ?
| |
| Abdulaziz Ghuloum 2004-11-10, 4:00 pm |
|
Phoenix77 wrote:
> (lambda (x)
> (set-car! x 3)))
When calling s, you pass the value of L, which is a pair whos car is the
symbol 'a'.
s gets that value and modifies its car field.
[color=darkred]
>
> (3 b c)
Correct, the car of that pair was modified.
> ; I found it different from C programming language
> ; why not return '(a b c) ?
this is very similar to the following C program:
void s (int* x){
x[0] = 3;
}
int main(int argc, char** argv){
int* x = {10, 20, 30}; /* using numbers instead of a,b,c */
s(x);
printf("x[0]=%d\n", x[0]);
}
Notice that even in the C version, you're passing arguments by value.
It's just that the value is a pointer (which is what scheme has).
> (lambda (x)
> (set! x '(x y z))))
>
Now s2 looks like:
void s2(int* x){
int* tmp = {10,20,30}; /* or do malloc/calloc/... */
x = tmp;
}
So, it does not modify the value of the original x.
>
> (3 b c)
And this matches the expectation.
> ; I am puzzled .
> ; According to the last function,
> ; I want to get the value '(x y z)
> ; but failed .
> ; Any one can tell me why? Thanks.
Did that explain why?
Aziz,,,
| |
| Abdulaziz Ghuloum 2004-11-10, 4:00 pm |
| Phoenix77 wrote:
> ; So I get a conclusion that the addresses of l and l2 are identical.
> ; Are they ?
If (eq? x y) returns true, then x and y are identical (have identical
addresses). Mutating one of them mutates the other. set!, btw, does
not mutate the value; it mutates the binding.
Aziz,,,
| |
| Antoun Kanawati 2004-11-10, 4:00 pm |
| Phoenix77 wrote:
> Hello,
> I writen 2 functions and was puzzled...
> ;Chez Scheme
> (lambda (x)
> (set-car! x 3)))
> (3 b c)
> ; I found it different from C programming language
> ; why not return '(a b c) ?
In C, you pass everything by value, but some values are pointers.
If this were C, and the cons-cell was something like the following:
struct cons {
void *car;
void *cdr;
};
Then,
(set-car! p FOO) <==> p->car = FOO
which mutates the value that p points to.
And
(set! p FOO) <==> p = FOO
which simply points p to some other place, and leaves the original
destination untouched.
> (lambda (x)
> (set! x '(x y z))))
>
> (3 b c)
> ; I am puzzled .
> ; According to the last function,
> ; I want to get the value '(x y z)
> ; but failed .
> ; Any one can tell me why? Thanks.
--
A. Kanawati
NO.antounk.SPAM@comcast.net
| |
| Jussi Piitulainen 2004-11-10, 4:00 pm |
| Phoenix77 writes:
> (lambda (x)
> (set-car! x 3)))
By the way, you are supposed to write
(define l (list 'a 'b 'c))
when you intend to mutate the list. Implementations can treat '(a b c)
as a literal, immutable constant. They need not enforce this.
|
|
|
|
|