Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

pass addresses or values? Help.
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.



Report this thread to moderator Post Follow-up to this message
Old Post
Phoenix77
11-10-04 09:00 PM


Re: pass addresses or values? Help.
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 ?



Report this thread to moderator Post Follow-up to this message
Old Post
Phoenix77
11-10-04 09:00 PM


Re: pass addresses or values? Help.
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.
 
>
> (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,,,

Report this thread to moderator Post Follow-up to this message
Old Post
Abdulaziz Ghuloum
11-10-04 09:00 PM


Re: pass addresses or values? Help.
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,,,


Report this thread to moderator Post Follow-up to this message
Old Post
Abdulaziz Ghuloum
11-10-04 09:00 PM


Re: pass addresses or values? Help.
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

Report this thread to moderator Post Follow-up to this message
Old Post
Antoun Kanawati
11-10-04 09:00 PM


Re: pass addresses or values? Help.
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Jussi Piitulainen
11-10-04 09:00 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Scheme archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:49 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.