Newsgroups: comp.lang.scheme
Path: cantaloupe.srv.cs.cmu.edu!das-news2.harvard.edu!news2.near.net!howland.reston.ans.net!news.moneng.mei.com!uwm.edu!spool.mu.edu!agate!darkstar.UCSC.EDU!news.hal.COM!decwrl!adobe!macb022.mv.us.adobe.com!user
From: mhamburg@mv.us.adobe.com (Mark Hamburg)
Subject: Re: call by value, again
Message-ID: <mhamburg-131194164808@macb022.mv.us.adobe.com>
Followup-To: comp.lang.scheme
Sender: usenet@adobe.com (USENET NEWS)
Organization: Adobe Systems, Inc.
References: <3a0mh8$prv@agate.berkeley.edu> <BLUME.94Nov11170247@dynamic.cs.princeton.edu> <3a5eff$k52@agate.berkeley.edu>
Date: Mon, 14 Nov 1994 00:48:08 GMT
Lines: 71

In article <3a5eff$k52@agate.berkeley.edu>, bh@anarres.CS.Berkeley.EDU
(Brian Harvey) wrote:

> [deleted material]

> What *I'm*
> trying to do is teach Scheme to someone who learned Pascal in high
> school, and who therefore believes that
> 
> 	In call by reference the called procedure can modify the
> 	value belonging to the caller;
> 
> 	In call by value the called procedure cannot modify the
> 	value belonging to the caller;
> 
> 	And that's the difference between them.
> 
> This hypothetical person has never even *heard* of call by name, not having
> been alive when the Algol 60 report came out.  Now along comes Scheme, and
> this person is confronted with the bizarre fact that some kinds of
> modification do affect the caller's value, whereas other kinds don't.  Such
> a person is not helped by being told that call by reference isn't
> interesting.  It's an open question, to me, whether such a person is helped
> by being told about environments and stores.
> 

But if you were implementing cons-pairs in Pascal, you would probably
implement them something like this:

TYPE
   Pair = ^PairDesc;
   PairDesc = RECORD first, rest : Pair END;

FUNCTION Cons (a, b: Pair) : Pair;
   VAR p : Pair;
   BEGIN
   New (p);
   p.first := a; p.rest := b;
   Cons := p
   END;

Using this system, we never pass around anything by reference in Pascal
terms -- i.e. use Pascal VAR parameters.  We always pass by value.  In
particular, here is set-car!:

PROCEDURE SetCar (a, b: Pair);
   BEGIN
   a.first := b;
   END;

Essentially, everything in Scheme is a pointer and pointers are always
passed by value.

If Scheme supported Pascal-style call-by-reference, we would be able to
write:

(define (increment (reference x)) (set! x (+ x 1)))

and then have the following sequence of statements:

(define y 3)
(increment y)

result in y being set to 4.

But you cannot do this in Scheme (at least without using special-forms
and/or macros) since everything is passed by value.

'nuff said.

Mark
