Re: on call by reference

Liste des GroupesRevenir à cl scheme 
Sujet : Re: on call by reference
De : alicetrillianosako (at) *nospam* gmail.com (Schol-R-LEA)
Groupes : comp.lang.scheme
Date : 20. Mar 2024, 16:57:18
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <utetgj$1hs9g$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
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.

Date Sujet#  Auteur
19 Mar 24 * on call by reference19Johanne Fairchild
20 Mar 24 +- Re: on call by reference1Lawrence D'Oliveiro
20 Mar 24 +* Re: on call by reference3Dmitri Volkov
20 Mar 24 i+- Re: on call by reference1Lawrence D'Oliveiro
20 Mar 24 i`- Re: on call by reference1Schol-R-LEA
20 Mar 24 +* Re: on call by reference4Alan Bawden
20 Mar 24 i`* Re: on call by reference3Johanne Fairchild
21 Mar 24 i `* Re: on call by reference2Alan Bawden
21 Mar 24 i  `- Re: on call by reference1Johanne Fairchild
20 Mar 24 `* Re: on call by reference10Schol-R-LEA
20 Mar 24  +* Re: on call by reference5Schol-R-LEA
21 Mar 24  i`* Re: on call by reference4Chris Vine
21 Mar 24  i `* Re: on call by reference3Lawrence D'Oliveiro
22 Mar 24  i  `* Re: on call by reference2Chris Vine
22 Mar 24  i   `- Re: on call by reference1Lawrence D'Oliveiro
21 Mar 24  +* Re: on call by reference2Alan Bawden
21 Mar 24  i`- Re: on call by reference1Schol-R-LEA
21 Mar 24  `* Re: on call by reference2Lawrence D'Oliveiro
21 Mar 24   `- Re: on call by reference1Schol-R-LEA

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal