Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
How to define a reference to another variable
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
carlmax  
View profile  
 More options Jul 23 2007, 10:39 am
Newsgroups: comp.lang.scheme
From: "carlmax" <carl...@tom.com>
Date: Mon, 23 Jul 2007 22:39:55 +0800
Local: Mon, Jul 23 2007 10:39 am
Subject: How to define a reference to another variable
Hi,
I'm confused about some scheme semantic, for example:
(define a (vector 1 2 3))
(define b a)
(vector-set! a 0 9)
(eq? a b)  ==> #t

(define a 1)
(define b a)
(set! a 9)
(eq? a b) ==> #f

then how to make b as a's alias, that is, after changing a, b is changed
accordingly?


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Bourguignon  
View profile  
 More options Jul 23 2007, 12:26 pm
Newsgroups: comp.lang.scheme
From: Pascal Bourguignon <p...@informatimago.com>
Date: Mon, 23 Jul 2007 18:26:44 +0200
Local: Mon, Jul 23 2007 12:26 pm
Subject: Re: How to define a reference to another variable

"carlmax" <carl...@tom.com> writes:
> Hi,
> I'm confused about some scheme semantic, for example:
> (define a (vector 1 2 3))
> (define b a)
> (vector-set! a 0 9)
> (eq? a b)  ==> #t

> (define a 1)
> (define b a)
> (set! a 9)
> (eq? a b) ==> #f

> then how to make b as a's alias, that is, after changing a, b is changed
> accordingly?

You cannot make aliases in scheme.  That's the point.

You can have objects, that are either mutable or immutable, and you
can bind these objects to variables.  Arguments are passed by values,
that is the object that is bound to a variable is passed as argument,
but since these are objects, in general, it's a reference to the
object that is passed internally.  Of course, in the case of immutable
objects, the compilers can optimize it and copy the object itself, but
the semantics are the same, this is invisible to the user.  Well,
invisible unless you use eq? which is an implementation dependant
operator that allows you to see when such copying is done.  Note that:

(define a 1)
(define b a)
(eqv? a b) ==> #t
(eq?  a b) ==> #t or #f depending on the implementation.

See:
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-9.html...

Note that when you call vector-set!, you are NOT modifying the binding
of b, but when you call set! you are modifying the binding of b.  So
you shouldn't be surprized that thereafter, eqv? or equal? return different
results. (You could be surprized if eq? returned same results).

Assume we define a functional vector-set:

(define (copy-vector v)
  (let loop ((r (make-vector (vector-length v)))
             (i 0))
     (if (>= i (vector-length v))
         r
         (begin (vector-set! r i (vector-ref v i))
                (loop r (+ 1 i))))))

(define (vector-set v i o)
  (let ((r (copy-vector v)))
     (vector-set! r i o)
     r))

Then we could get similar results, using similar operations:

The only difference is that a vector is mutable: you can modify parts
of its data without modifying the object identity of the vector, while
integers are immutable: you cannot modify part of an integer.

You should also read: http://www.nhplace.com/kent/PS/EQUAL.html

Ok, but now if what you want to do is to really get aliasing behavior,
you can still implement this abstraction, but not using set!, you'll
have to use alias-set!.  What you need to do, is to store the values,
not as variable binding, but inside a mutable object.  For this, you
can use a vector, a cons cell, a structure, etc.

For example, with conses:

(define (make-alias value)   (cons 'alias value))
(define (alias-get alias)    (cdr alias))
(define (alias-set! alias v) (set-cdr! alias v))

--
__Pascal Bourguignon__                     http://www.informatimago.com/

HANDLE WITH EXTREME CARE: This product contains minute electrically
charged particles moving at velocities in excess of five hundred
million miles per hour.


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Costanza  
View profile  
 More options Jul 23 2007, 12:33 pm
Newsgroups: comp.lang.scheme
From: Pascal Costanza <p...@p-cos.net>
Date: Mon, 23 Jul 2007 18:33:58 +0200
Local: Mon, Jul 23 2007 12:33 pm
Subject: Re: How to define a reference to another variable

Currently, identifier macros are being considered for inclusion in R6RS
Scheme (also known as symbol macros in Common Lisp). They would enable
aliases.

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
carlmax  
View profile  
 More options Jul 23 2007, 7:17 pm
Newsgroups: comp.lang.scheme
From: "carlmax" <carl...@tom.com>
Date: Tue, 24 Jul 2007 07:17:47 +0800
Local: Mon, Jul 23 2007 7:17 pm
Subject: Re: How to define a reference to another variable
"Pascal Bourguignon" <p...@informatimago.com> write
<news:87644bf5mj.fsf@voyager.informatimago.com>...

> The only difference is that a vector is mutable: you can modify parts
> of its data without modifying the object identity of the vector, while
> integers are immutable: you cannot modify part of an integer.

I have never heard about mutable and immutable object. Thanks you very much.
I've just finihed HtDP, and there is no sentence about object.
I guess, most compound objects, like structure, list, vector are mutable,
and others
are immutable, am I right?
I'll read it.
Thanks again.

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Pascal Bourguignon  
View profile  
 More options Jul 24 2007, 8:08 am
Newsgroups: comp.lang.scheme
From: Pascal Bourguignon <p...@informatimago.com>
Date: Tue, 24 Jul 2007 14:08:41 +0200
Local: Tues, Jul 24 2007 8:08 am
Subject: Re: How to define a reference to another variable

"carlmax" <carl...@tom.com> writes:
> "Pascal Bourguignon" <p...@informatimago.com> write
> <news:87644bf5mj.fsf@voyager.informatimago.com>...

>> The only difference is that a vector is mutable: you can modify parts
>> of its data without modifying the object identity of the vector, while
>> integers are immutable: you cannot modify part of an integer.

> I have never heard about mutable and immutable object. Thanks you very much.
> I've just finihed HtDP, and there is no sentence about object.
> I guess, most compound objects, like structure, list, vector are mutable,
> and others
> are immutable, am I right?

Basically, yes.

Note that you should also consider as immutable any quoted or literal
object.

(let ((mutable-v   (vector 1 2 3))
      (immutable-v #(1 2 3))
      (mutable-l   (list 1 2 3))
      (immutable-l '(1 2 3))
      (mutable-s   (make-string 3 #\a))
      (immutable-s "aaa"))
  (etc))

See "Storage Model":
http://www.schemers.org/Documents/Standards/R5RS/HTML/r5rs-Z-H-6.html...

--
__Pascal Bourguignon__                     http://www.informatimago.com/

"Remember, Information is not knowledge; Knowledge is not Wisdom;
Wisdom is not truth; Truth is not beauty; Beauty is not love;
Love is not music; Music is the best." -- Frank Zappa


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Anton van Straaten  
View profile  
 More options Jul 24 2007, 1:40 pm
Newsgroups: comp.lang.scheme
From: Anton van Straaten <an...@appsolutions.com>
Date: Tue, 24 Jul 2007 17:40:00 GMT
Subject: Re: How to define a reference to another variable

carlmax wrote:
> "Pascal Bourguignon" <p...@informatimago.com> write
> <news:87644bf5mj.fsf@voyager.informatimago.com>...

>>The only difference is that a vector is mutable: you can modify parts
>>of its data without modifying the object identity of the vector, while
>>integers are immutable: you cannot modify part of an integer.

> I have never heard about mutable and immutable object. Thanks you very much.
> I've just finihed HtDP, and there is no sentence about object.

HtDP talks about "mutable compound values".  Part VIII deals with
"Changing Compound Values", with chapter 40 covering "Mutable
Structures" and chapter 43 "Changing Structures, Vectors, and Objects".

See
http://www.htdp.org/2003-09-26/Book/curriculum-Z-H-1.html#node_toc_no...

Anton


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google