Deutsch   English   Français   Italiano  
<uqkfsm$37237$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!news.mixmin.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Schol-R-LEA <alicetrillianosako@gmail.com>
Newsgroups: comp.lang.scheme
Subject: Re: on call by reference
Date: Wed, 20 Mar 2024 10:57:18 -0400
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <utetgj$1hs9g$1@dont-email.me>
References: <877chyiosp.fsf@tudado.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Wed, 20 Mar 2024 14:57:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="66d48328225f3a70f5d6636ec8b98f1b";
	logging-data="1634608"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+JP+F6uPvm+3VwvA5xYFlnGWzI2U5hl6k="
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:aMRbZP03qbydCI4Z1atCOjPt/Ug=
Content-Language: en-US
In-Reply-To: <877chyiosp.fsf@tudado.org>
Bytes: 4727

Johanne Fairchild:
> I tried to answer whether Scheme was call-by-reference and I did not
> think the definition of call-by-reference seen on the web is precise
> enough.  

Scheme is call-by-value; arguments to procedures are copied into the 
parameters in the procedure's environment. As I understand it, even when 
the argument is a reference - such as with a list - a full copy of the 
argument is made local to the procedure.

Each instance where a form alters a value, rather than returning a new 
one, the form is either a primitive form (e.g., (set!), (set-car!), 
(set-cdr!)) or a macro (in which the body of the macro is expanded into 
the code in place; that is to say, the generated code replaces the macro 
invocation). The same holds for cases where an argument is conditionally 
evaluated (e.g., the consequent and alternative in an (if) form).

> For instance,
> 
>    Call by reference (or pass by reference) is an evaluation strategy
>    where a parameter is bound to an implicit reference to the variable
>    used as argument, rather than a copy of its value. This typically
>    means that the function can modify (i.e., assign to) the variable used
>    as argument—something that will be seen by its caller.
> 
>    Source:
>    https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference
> 
> It doesn't say how the modification is done.  

In call-by-reference, what is passed is a pointer or similar reference 
to the

Very, very few current languages support call-by-reference, and those 
which do generally do so explicitly, such as with Pascal's VAR 
parameters. Even some which allow passing of mutable variables do not 
actually use call-by-reference semantics - Ada, for example, IIUC is 
better described as using call-by-value-and-copy semantics for 'out' and 
'in out' parameters, though the actual implementation may and often does 
use call-by-reference internally, so long as it behaves as if it is 
copying the modified value back to the argument.

Languages such as C and C++ are interesting in this regard, as they have 
only call-by-value semantics, but also support passing explicit 
pointers, which allows them to simulate call-by-reference semantics by 
the passing of explicit pointers.

Similarly, in languages such as Java or Python, call-by-value is used to 
pass variables, whether they hold primitives or object references; but 
since object variables themselves are references (usually, but not 
necessarily, implemented as pointers) to heap objects, it allows for the 
referred object to be modified by the referring variables.

> So we can say that Python
> is call-by-reference, but surely not when the data is immutable---then
> Python is sometimes call-by-reference. 

Python is always call-by-value; however, in some cases, the 'value' is 
itself a reference.

> Just this observation already
> makes a language sometimes call-by-reference and sometimes not.  So
> 
>    ``Is Scheme call-by-reference?''
> 
> would not make any sense.  We can change data by way of its
> argument---set-car!, say.  On the other hand, in Scheme arguments are
> passed with an implicit reference to the variable, so it is
> call-by-reference, except perhaps when the argument is immutable.

As I said earlier, Scheme always uses call-by-value semantics in 
procedures (one could argue that 'classical' Lisp macros use 
call-by-name semantics, whereas hygienic macros use a complex 
combination of call-by-value and call-by-name, but I will leave that to 
others to debate). Only primitive forms and macros can actually modify a 
value, rather than returning a new one.

Comments and corrections welcome.