From: BubbaFrench
Subject: Pass-by-value
Date: 
Message-ID: <9e50adaa-ea59-4d2e-88c5-937a24254d55@k38g2000yqh.googlegroups.com>
Hello:)

My question is - when objects in lisp are passed by reference, and
when they are by value. Objects - I mean not only CLOS instances, just
everything, that can be passed.

Thank you)

From: BubbaFrench
Subject: Re: Pass-by-value
Date: 
Message-ID: <6f8ac71b-083e-4824-a0fa-01e732f202b7@s20g2000yqh.googlegroups.com>
On 8 ÁÐÒ, 10:08, BubbaFrench <···········@gmail.com> wrote:
> Hello:)
>
> My question is - when objects in lisp are passed by reference, and
> when they are by value. Objects - I mean not only CLOS instances, just
> everything, that can be passed.
>
> Thank you)

I ask that, because different sources say different things. And it is
somewhat confusing.
From: BubbaFrench
Subject: Re: Pass-by-value
Date: 
Message-ID: <2bea7a5b-51c2-4009-8610-869f12d5e6ea@k2g2000yql.googlegroups.com>
On 8 ÁÐÒ, 10:08, BubbaFrench <···········@gmail.com> wrote:
> Hello:)
>
> My question is - when objects in lisp are passed by reference, and
> when they are by value. Objects - I mean not only CLOS instances, just
> everything, that can be passed.
>
> Thank you)

Ok, my first observation is:

1. On the CLISP main page, they say that "Like Java, Lisp is pass-by-
value", and demonstrate, that integer argument, passed to the function
will not be modified, whatever the function does.

2. However, when I pass a structure to my function in LispWorks
(structure made with make-foo, modified with (setf (foo-value foo) 1))
in function body), then my function can modify it, so the value slot
of foo changes.

If that is normal, I suppose, that structures are passed by reference
in lisp.
From: ·············@gmail.com
Subject: Re: Pass-by-value
Date: 
Message-ID: <7c3a0378-daab-4e7f-86e4-49884a6352e3@e21g2000vbe.googlegroups.com>
On Apr 8, 8:25 am, BubbaFrench <···········@gmail.com> wrote:
> On 8 ÁÐÒ, 10:08, BubbaFrench <···········@gmail.com> wrote:
>

>
> If that is normal, I suppose, that structures are passed by reference
> in lisp.
If you google this group you'll find a lot of threads regarding this
problem (try also call by identity). Pragmatically I liked the
folowing statement:
Lisp uses pass-by-value semantics:  the callee cannot modify any
binding
of name to value in the caller.  However, nearly *everything* in Lisp
is passed as an aggregate, so while you cannot change someone else's
bindings, you *can* change the *contents* of the aggregate.

Karsten
From: BubbaFrench
Subject: Re: Pass-by-value
Date: 
Message-ID: <3dd9c529-6ba5-4209-8fcb-f7e81d5c3c93@q9g2000yqc.googlegroups.com>
On 8 апр, 11:21, ·············@gmail.com wrote:
> On Apr 8, 8:25 am, BubbaFrench <···········@gmail.com> wrote:
>
> > On 8 ÁÐÒ, 10:08, BubbaFrench <···········@gmail.com> wrote:
>
> > If that is normal, I suppose, that structures are passed by reference
> > in lisp.
>
> If you google this group you'll find a lot of threads regarding this
> problem (try also call by identity). Pragmatically I liked the
> folowing statement:
> Lisp uses pass-by-value semantics:  the callee cannot modify any
> binding
> of name to value in the caller.  However, nearly *everything* in Lisp
> is passed as an aggregate, so while you cannot change someone else's
> bindings, you *can* change the *contents* of the aggregate.
>
> Karsten

Ok, thank you:)
From: Barry Margolin
Subject: Re: Pass-by-value
Date: 
Message-ID: <barmar-FEE971.15113808042009@mara100-84.onlink.net>
In article 
<····································@e21g2000vbe.googlegroups.com>,
 ·············@gmail.com wrote:

> On Apr 8, 8:25�am, BubbaFrench <···········@gmail.com> wrote:
> > On 8 ���, 10:08, BubbaFrench <···········@gmail.com> wrote:
> >
> 
> >
> > If that is normal, I suppose, that structures are passed by reference
> > in lisp.
> If you google this group you'll find a lot of threads regarding this
> problem (try also call by identity). Pragmatically I liked the
> folowing statement:
> Lisp uses pass-by-value semantics:  the callee cannot modify any
> binding
> of name to value in the caller.  However, nearly *everything* in Lisp
> is passed as an aggregate, so while you cannot change someone else's
> bindings, you *can* change the *contents* of the aggregate.
> 
> Karsten

A better way to say it may be that everything is passed by value, but 
the values themselves are references.

This is the way it is conceptually, at least.  As another response 
mentioned, there's a distinction for some simple types.  These types are 
immutable, i.e. there are no operations to modify their contents (you 
can't change a bit in a fixnum, you can only calculate a new fixnum with 
the bit changed).  The implementation is allowed to optimize them and 
pass them as immediate data, rather than as references to aggregates.  
This is why we have EQ and EQL -- the former operates on the raw data, 
the latter on the conceptual data (e.g. all integers with the same value 
are treated as the same aggregate object).

-- 
Barry Margolin, ······@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
From: gugamilare
Subject: Re: Pass-by-value
Date: 
Message-ID: <62eb96c1-2818-43bb-98f4-813ce80ade69@f19g2000yqo.googlegroups.com>
On 8 abr, 03:25, BubbaFrench <···········@gmail.com> wrote:
> On 8 ÁÐÒ, 10:08, BubbaFrench <···········@gmail.com> wrote:
>
> > Hello:)
>
> > My question is - when objects in lisp are passed by reference, and
> > when they are by value. Objects - I mean not only CLOS instances, just
> > everything, that can be passed.
>
> > Thank you)
>
> Ok, my first observation is:
>
> 1. On the CLISP main page, they say that "Like Java, Lisp is pass-by-
> value", and demonstrate, that integer argument, passed to the function
> will not be modified, whatever the function does.
>
> 2. However, when I pass a structure to my function in LispWorks
> (structure made with make-foo, modified with (setf (foo-value foo) 1))
> in function body), then my function can modify it, so the value slot
> of foo changes.
>
> If that is normal, I suppose, that structures are passed by reference
> in lisp.

I always separate between imediate and non-imediate values. Imediate
values are numbers, characters and symbols. Others are non-imediate.

Lisp objects are never passed by reference, but also no fresh copies
are made at all. You can never modify anything by changing the
variable's value. For instance, this

(defun foo (value)
  (setf value (get-new-value)))

won't do anything good for any kind of object, it will just return the
value returned by (get-new-value). OTOH, if you do

(defun foo (list)
  (setf (car list) (get-new-value)))

then the car of anything you pass to foo will be changed to a new
value, because a cons cell can be compared to a pointer for a
structure (like in C) of two fiels, the car and the cdr. If you change
car's or cdr's value, it will be changed in place. This will happen
for any non-imediate values (i.e. values that have fields).

I hope this explanation was clear.

Gustavo.
From: BubbaFrench
Subject: Re: Pass-by-value
Date: 
Message-ID: <7de55c52-5bde-4361-bc7d-e04f38395820@h28g2000yqd.googlegroups.com>
On 8 апр, 17:09, gugamilare <··········@gmail.com> wrote:
> On 8 abr, 03:25, BubbaFrench <···········@gmail.com> wrote:
>
>
>
>
>
> > On 8 ÁÐÒ, 10:08, BubbaFrench <···········@gmail.com> wrote:
>
> > > Hello:)
>
> > > My question is - when objects in lisp are passed by reference, and
> > > when they are by value. Objects - I mean not only CLOS instances, just
> > > everything, that can be passed.
>
> > > Thank you)
>
> > Ok, my first observation is:
>
> > 1. On the CLISP main page, they say that "Like Java, Lisp is pass-by-
> > value", and demonstrate, that integer argument, passed to the function
> > will not be modified, whatever the function does.
>
> > 2. However, when I pass a structure to my function in LispWorks
> > (structure made with make-foo, modified with (setf (foo-value foo) 1))
> > in function body), then my function can modify it, so the value slot
> > of foo changes.
>
> > If that is normal, I suppose, that structures are passed by reference
> > in lisp.
>
> I always separate between imediate and non-imediate values. Imediate
> values are numbers, characters and symbols. Others are non-imediate.
>
> Lisp objects are never passed by reference, but also no fresh copies
> are made at all. You can never modify anything by changing the
> variable's value. For instance, this
>
> (defun foo (value)
>   (setf value (get-new-value)))
>
> won't do anything good for any kind of object, it will just return the
> value returned by (get-new-value). OTOH, if you do
>
> (defun foo (list)
>   (setf (car list) (get-new-value)))
>
> then the car of anything you pass to foo will be changed to a new
> value, because a cons cell can be compared to a pointer for a
> structure (like in C) of two fiels, the car and the cdr. If you change
> car's or cdr's value, it will be changed in place. This will happen
> for any non-imediate values (i.e. values that have fields).
>
> I hope this explanation was clear.
>
> Gustavo.- Скрыть цитируемый текст -
>
> - Показать цитируемый текст -

Yes, clear enough.
From: Kaz Kylheku
Subject: Re: Pass-by-value
Date: 
Message-ID: <20090418031249.867@gmail.com>
On 2009-04-08, BubbaFrench <···········@gmail.com> wrote:
> Hello:)
>
> My question is - when objects in lisp are passed by reference, and
> when they are by value. Objects - I mean not only CLOS instances, just
> everything, that can be passed.

Lisp function arguments are values; Lisp has no reference arguments like VAR
parameters in Pascal, or the address-of operator in C in combination with
pointer parameters.

If F is a function, and P is an expression denoting some storage location
(i.e. a place), then (F P) has no way of updating the place. Prior to the
invocation of F, the expresion P is reduced to the value contained in P,
and this value is passed to F.

Values in Lisp are often references, but that's a different sense from
``reference parameter'': it's about the relationship between the value and what
it represents, not between an argument expression and a function call!

In Lisp the syntax (F P) may mutate place P, if F is defined as a macro
operator, or if F is a special form. So there is a kind of call-by-reference
possible in Lisp, just not in function calls!

Lisp is powerful enough, though, that you can create a data type whose values
serve as references to assignable places,  such as the slots of a structure or
array, or even lexical variables. These references are first class values: they
can be passed anywhere, and dereferenced to gain access to the original place.
An example of such a module is here:

  http://paste.lisp.org/display/71952

With these macros, you can for istance write:

   (defun store-42-into-anything (r)
     (setf (deref r) 42))

   (let ((cell (cons 1 2))
         (x 3))
     (store-42-into-anything (ref (cdr cell)))
     (store-42-into-anything (ref (car cell)))
     (store-42-into-anything (ref x))
     (values cell x))

   -> (42 . 42) ; 42

Of course, this doesn't add reference parameters to Lisp function calls, but it
gives you a way of doing the same thing, similarly to how explicit pointers and
address-taking is used in C to simulate reference parameters.