Re: on call by reference

Liste des GroupesRevenir à cl scheme 
Sujet : Re: on call by reference
De : chris (at) *nospam* cvine--nospam--.freeserve.co.uk (Chris Vine)
Groupes : comp.lang.scheme
Date : 21. Mar 2024, 20:08:12
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240321180812.830e9559eb6b35aae14dc29b@cvine--nospam--.freeserve.co.uk>
References : 1 2 3
User-Agent : Sylpheed 3.7.0 (GTK+ 2.24.33; x86_64-unknown-linux-gnu)
On Wed, 20 Mar 2024 11:07:36 -0400
Schol-R-LEA <alicetrillianosako@gmail.com> wrote:
I apologize, I lost my train of thought for part of this:
 
Schol-R-LEA:
 
In call-by-reference, what is passed is a pointer or similar reference
to the
 
...to the variable.
 
In effect, a language in which call-by-reference is used, parameters are
implicit pointers to the arguments. So, by comparison to C,
 
void foo(int bar)
{
    bar = 23;
}
 
in a call-by-reference dialect would be equivalent to
 
void foo(int* bar)
{
    *bar = 23;
}
 
in standard C.
 
For obvious reasons, this presents problems with potential side effects
and is harmful to modularity, as any change to the parameter variable
would silently change the argument. This is why most languages which
support call-by-reference at all do so explicitly, with call-by-value
being the default.

However that analysis does not help you very much with scheme.  Scheme
is call by value but for mutable entities it behaves like call by
reference, in that the thing passed by value is analogous to a
pointer.  Scheme is call by value in the sense that all the arguments
of a procedure must be evaluated before the body of the procedure is
entered; and any "reseating" of an identifier within a procedure does
not reseat the reference viewed lexically outside the procedure (see
below).

Let's first deal with a preliminary issue.  In scheme, set! does not
mutate any value.  Instead it "reseats" an identifier name so that that
identifier refers to a different entity than the one it previously
referred to.  The thing previously referred to is unchanged, except
that if no other identifier refers to it it might be garbage collected
as unreachable.  Take this simple example:

(let* ([a '(x)]
       [b a]
       [change (lambda (arg) (set! arg 2))])
  (write a)(newline)
  (write b)(newline)
  (change a)  ;; outside the change procedure neither a nor b is changed
  (write a)(newline)
  (write b)(newline))

However entities which may be mutated are as if passed by pointer.
Here is an example of a list:

(let* ([a (list `x)]
       [b a]
       [change (lambda (arg) (set-car! arg 'y))])
  (write a)(newline)
  (write b)(newline)
  (change a)  ;; changes the list referred to by a and b
  (write a)(newline)
  (write b)(newline))

And here is an example of a vector:

(let* ([a (vector 'x 'y)]
       [b a]
       [change (lambda (arg) (vector-set! arg 1 'z))])
  (write a)(newline)
  (write b)(newline)
  (change a)  ;; changes the vector referred to by a and b
  (write a)(newline)
  (write b)(newline))

This is reflected in the way many schemes are implemented.  Scalar-like
things, such as booleans, symbols and integers fitting within 31 or 63
bits are often "unboxed" or "immediate", that is they are not allocated
on the garbage collected heap.  More complicated things such as lists
or vectors are usually "boxed".  This arrangement is often implemented
by pointer tagging (https://en.wikipedia.org/wiki/Tagged_pointer).

Chris

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