From: ···············@free.fr
Subject: rplaca return value ?
Date: 
Message-ID: <1148325862.820291.195990@y43g2000cwc.googlegroups.com>
Hi !

The side effect rplaca does is clear.
But, If you write a LISP, what would be the return value you give to
rplaca  ?

Thanks.

Fabrice

From: Peter Seibel
Subject: Re: rplaca return value ?
Date: 
Message-ID: <m2u07hsuxi.fsf@gigamonkeys.com>
···············@free.fr writes:

> Hi !
>
> The side effect rplaca does is clear. But, If you write a LISP, what
> would be the return value you give to rplaca ?

Well, Common Lisp returns the modified cons. But if you're going to
keep the notion of a generalized assignment operator, a la SETF, then
you might say, well RPLACA is just a primitive operation provided to
implement (SETF CAR) and thus might as well return the value needed by
SETF, namely the assigned value.

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Pascal Bourguignon
Subject: Re: rplaca return value ?
Date: 
Message-ID: <877j4d7x2k.fsf@thalassa.informatimago.com>
Peter Seibel <·····@gigamonkeys.com> writes:
> ···············@free.fr writes:
>> The side effect rplaca does is clear. But, If you write a LISP, what
>> would be the return value you give to rplaca ?
>
> Well, Common Lisp returns the modified cons. But if you're going to
> keep the notion of a generalized assignment operator, a la SETF, then
> you might say, well RPLACA is just a primitive operation provided to
> implement (SETF CAR) and thus might as well return the value needed by
> SETF, namely the assigned value.

This is not possible. CL:RPLACA is specified to return a CONS.
If you write (CL:RPLACA (CL:CONS 1 2) 3), it cannot return 3, but
(CL:SETF (CL:CAR (CL:CONS 1 2)) 3) must return 3.

However, formally, the standard doesn't prevent an implementation to return:

(CL:RPLACA (CL:CONS 1 2) 3) --> (ZAPHOD . BEEBLEBROX)

Formally, it's only specified to return A cons.

(Of course, I'd prefer to use an implementation that would return THE
cons passed as first argument, and to use a "Common Lisp 2010"
standard that would specify it).

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

NEW GRAND UNIFIED THEORY DISCLAIMER: The manufacturer may
technically be entitled to claim that this product is
ten-dimensional. However, the consumer is reminded that this
confers no legal rights above and beyond those applicable to
three-dimensional objects, since the seven new dimensions are
"rolled up" into such a small "area" that they cannot be
detected.
From: Peter Seibel
Subject: Re: rplaca return value ?
Date: 
Message-ID: <m2hd3gst2k.fsf@gigamonkeys.com>
Pascal Bourguignon <···@informatimago.com> writes:

> Peter Seibel <·····@gigamonkeys.com> writes:
>> ···············@free.fr writes:
>>> The side effect rplaca does is clear. But, If you write a LISP, what
>>> would be the return value you give to rplaca ?
>>
>> Well, Common Lisp returns the modified cons. But if you're going to
>> keep the notion of a generalized assignment operator, a la SETF, then
>> you might say, well RPLACA is just a primitive operation provided to
>> implement (SETF CAR) and thus might as well return the value needed by
>> SETF, namely the assigned value.
>
> This is not possible. CL:RPLACA is specified to return a CONS.

That depends on whether we're talking about Common Lisp or another
Lisp dialect. Since the OP asked about "a LISP" I assume he's not
talking about Common Lisp. Thus my answer. If he was asking about
Common Lisp then obviously the answer is to do what the language
standard says.

> If you write (CL:RPLACA (CL:CONS 1 2) 3), it cannot return 3, but
> (CL:SETF (CL:CAR (CL:CONS 1 2)) 3) must return 3.
>
> However, formally, the standard doesn't prevent an implementation to return:
>
> (CL:RPLACA (CL:CONS 1 2) 3) --> (ZAPHOD . BEEBLEBROX)
>
> Formally, it's only specified to return A cons.

Actually I'm not sure that's true. They use the same meta-syntactic
variable, cons, on both sides:

  rplaca cons object => cons

I think for it be interpreted the way you're suggesting they'd have
had to write:

  rplaca cons object => result-cons

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: Pascal Bourguignon
Subject: Re: rplaca return value ?
Date: 
Message-ID: <877j4c7e88.fsf@thalassa.informatimago.com>
Peter Seibel <·····@gigamonkeys.com> writes:
> Actually I'm not sure that's true. They use the same meta-syntactic
> variable, cons, on both sides:
>
>   rplaca cons object => cons
>
> I think for it be interpreted the way you're suggesting they'd have
> had to write:
>
>   rplaca cons object => result-cons

Oops, you're right.

I never realized that these parameter names were more value-like than
type-like.

Indeed it means that rplaca returns its first argument.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

The world will now reboot.  don't bother saving your artefacts.
From: ···············@free.fr
Subject: Re: rplaca return value ?
Date: 
Message-ID: <1148418047.126100.140370@i40g2000cwc.googlegroups.com>
Thank you for the explanations.

> I assume he's not talking about Common Lisp
You are right, I wasn't. I only wonder, for a small experimental LISP,
what could be the most easily usable return value of rplaca.

If I've understood : rplaca should return the car-modified cons.
From: Barry Margolin
Subject: Re: rplaca return value ?
Date: 
Message-ID: <barmar-0F8329.17112523052006@comcast.dca.giganews.com>
In article <························@i40g2000cwc.googlegroups.com>,
 ···············@free.fr wrote:

> Thank you for the explanations.
> 
> > I assume he's not talking about Common Lisp
> You are right, I wasn't. I only wonder, for a small experimental LISP,
> what could be the most easily usable return value of rplaca.

It probably doesn't matter.  RPLACA is mostly used for its side effect, 
rarely for value.

-- 
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: Pascal Costanza
Subject: Re: rplaca return value ?
Date: 
Message-ID: <4dhe4lF1a5ptdU1@individual.net>
Pascal Bourguignon wrote:
> Peter Seibel <·····@gigamonkeys.com> writes:
>> Actually I'm not sure that's true. They use the same meta-syntactic
>> variable, cons, on both sides:
>>
>>   rplaca cons object => cons
>>
>> I think for it be interpreted the way you're suggesting they'd have
>> had to write:
>>
>>   rplaca cons object => result-cons
> 
> Oops, you're right.
> 
> I never realized that these parameter names were more value-like than
> type-like.
> 
> Indeed it means that rplaca returns its first argument.

No, it doesn't.

The source code of the relevant section for the ANSI CL specification 
looks like this:

==================
%% 15.3.0 4
\funref{rplaca} replaces the \term{car} of the \param{cons} with 
\param{object}.
%and then returns the modified \param{cons}.

\funref{rplacd} replaces the \term{cdr} of the \param{cons} with 
\param{object}.
%and then returns the modified \param{cons}.
==================

So the part that explicitly mentions that the modified cons is returned 
was explicitly commented out.

My guess is that the goal was to make it unspecified what the return 
value is, but that that goal wasn't completely achieved. As Barry said, 
the return value isn't really important here...


Pascal

-- 
3rd European Lisp Workshop
July 3 - Nantes, France - co-located with ECOOP 2006
http://lisp-ecoop06.bknr.net/
From: Peter Seibel
Subject: Re: rplaca return value ?
Date: 
Message-ID: <m2zmh8quir.fsf@gigamonkeys.com>
Pascal Costanza <··@p-cos.net> writes:

> Pascal Bourguignon wrote:
>> Peter Seibel <·····@gigamonkeys.com> writes:
>>> Actually I'm not sure that's true. They use the same meta-syntactic
>>> variable, cons, on both sides:
>>>
>>>   rplaca cons object => cons
>>>
>>> I think for it be interpreted the way you're suggesting they'd have
>>> had to write:
>>>
>>>   rplaca cons object => result-cons
>> Oops, you're right.
>> I never realized that these parameter names were more value-like
>> than
>> type-like.
>> Indeed it means that rplaca returns its first argument.
>
> No, it doesn't.
>
> The source code of the relevant section for the ANSI CL specification
> looks like this:
>
> ==================
> %% 15.3.0 4
> \funref{rplaca} replaces the \term{car} of the \param{cons} with
> \param{object}.
> %and then returns the modified \param{cons}.
>
> \funref{rplacd} replaces the \term{cdr} of the \param{cons} with
> \param{object}.
> %and then returns the modified \param{cons}.
> ==================
>
> So the part that explicitly mentions that the modified cons is
> returned was explicitly commented out.

Well, maybe that's just because they moved the mention the side
effects down into the "Side Effects" section. I'm not saying that's
necessarily what happened; just that it's hard enough to interpret
specifications without guessing at what the meaning of having certain
bits commented out is.

-Peter

-- 
Peter Seibel           * ·····@gigamonkeys.com
Gigamonkeys Consulting * http://www.gigamonkeys.com/
Practical Common Lisp  * http://www.gigamonkeys.com/book/
From: ···············@free.fr
Subject: Re: rplaca return value ?
Date: 
Message-ID: <1148423503.884535.18530@j33g2000cwa.googlegroups.com>
  Probably I was influenced by C language were return value of
functions are carefully choosen for the case one want to nest the call
inside a one piece compound statement. Maybe in LISP these kind of
practice aren't very useful...
From: ···············@free.fr
Subject: Re: rplaca return value ?
Date: 
Message-ID: <1148423671.249330.155230@i39g2000cwa.googlegroups.com>
Spoke about a side effect function.
From: Arseny Slobodyuk
Subject: Re: rplaca return value ?
Date: 
Message-ID: <1148437895.289901.41500@y43g2000cwc.googlegroups.com>
···············@free.fr wrote:
> Probably I was influenced by C language were return value of
> functions are carefully choosen for the case one want to nest the call
> inside a one piece compound statement. Maybe in LISP these kind of
> practice aren't very useful...

I guess you're joking here. Lisp is often called functional language,
that means it encourages nonprocedural programming, i.e. having less
variables, nested function calling. OTOH, one may program procedurally
in Lisp as well.
From: ···············@free.fr
Subject: Re: rplaca return value ?
Date: 
Message-ID: <1148485540.561325.98040@i39g2000cwa.googlegroups.com>
>You can call:  (RPLACD (RPLACA some-cons new-car) new-cdr)
Thanks, I see here how the returned RPLACA value can be used.
I must modify my tiny LISP according to this. ( I returned the second
arg, not the cons.)

> I guess you're joking here. Lisp is often called functional language, that means...
It's for that reason I added I was only speaking about side effect
functions.
  I do know that functional programming heavily relies on the results
of nested functions.
From: Barry Margolin
Subject: Re: rplaca return value ?
Date: 
Message-ID: <barmar-76A4AC.16315124052006@comcast.dca.giganews.com>
In article <·······················@y43g2000cwc.googlegroups.com>,
 "Arseny Slobodyuk" <····@users.sourceforge.net> wrote:

> ···············@free.fr wrote:
> > Probably I was influenced by C language were return value of
> > functions are carefully choosen for the case one want to nest the call
> > inside a one piece compound statement. Maybe in LISP these kind of
> > practice aren't very useful...
> 
> I guess you're joking here. Lisp is often called functional language,
> that means it encourages nonprocedural programming, i.e. having less
> variables, nested function calling. OTOH, one may program procedurally
> in Lisp as well.

While it's often called a functional language, that's not how it's 
usually used, and calling it functional doesn't make it so.  Its only 
"functional" in the sense that every statement is an expression with a 
value, so they *can* be used for their return value if you want.

-- 
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: Arseny Slobodyuk
Subject: Re: rplaca return value ?
Date: 
Message-ID: <1148518648.883893.194770@g10g2000cwb.googlegroups.com>
> While it's often called a functional language, that's not how it's
> usually used, and calling it functional doesn't make it so.  Its only
> "functional" in the sense that every statement is an expression with a
> value, so they *can* be used for their return value if you want.

Well, I've heard there are more `functional' languages in this sense
(not in the sense oppositional to `nonfunctional' heh), particularly,
Haskell, but never learned and used them except Prolog (long time ago).
So I feel the need to defend Lisp slightly and remember also VALUES and
ability to easily construct new functions.
From: Pascal Bourguignon
Subject: Re: rplaca return value ?
Date: 
Message-ID: <87lkss5p6g.fsf@thalassa.informatimago.com>
···············@free.fr writes:
>   Probably I was influenced by C language were return value of
> functions are carefully choosen for the case one want to nest the call
> inside a one piece compound statement. Maybe in LISP these kind of
> practice aren't very useful...

This is not the question. 

The problem was with the way the specifications are written.  One
objective of the way they were written is to avoid any redundancy, to
avoid any inconsistency. So they commented out a sentence since its
meaning was already conveyed "formally" by the specification of the
parameters and result of the function, which we mustn't overlook.

So in Common Lisp, the result of RPLACA is clearly specified to be its
first argument.

You can call:  (RPLACD (RPLACA some-cons new-car) new-cdr)

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

This universe shipped by weight, not volume.  Some expansion may have
occurred during shipment.
From: Barry Margolin
Subject: Re: rplaca return value ?
Date: 
Message-ID: <barmar-6513C8.16270524052006@comcast.dca.giganews.com>
In article <·······················@j33g2000cwa.googlegroups.com>,
 ···············@free.fr wrote:

>   Probably I was influenced by C language were return value of
> functions are carefully choosen for the case one want to nest the call
> inside a one piece compound statement. Maybe in LISP these kind of
> practice aren't very useful...

Some were considered more carefully than others by the Common Lisp 
designers.  In this case, Common Lisp simply copied what MACLISP had 
been doing for over a decade, which it may have inherited from LISP 1.5.  
Who knows how much consideration was given to this detail way back in 
the 60's?

I'm having a hard time coming up with any likely code that would nest a 
call to RPLACA/RPLACD, whether it returns the new value or the cons.  It 
might be useful in an Obfuscated Lisp Code Contest, though.

-- 
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: Johan Bockgård
Subject: Re: rplaca return value ?
Date: 
Message-ID: <yoijr72i9507.fsf@linus003.dd.chalmers.se>
Barry Margolin <······@alum.mit.edu> writes:

> Some were considered more carefully than others by the Common Lisp
> designers. In this case, Common Lisp simply copied what MACLISP had
> been doing for over a decade, which it may have inherited from LISP
> 1.5. Who knows how much consideration was given to this detail way
> back in the 60's?

Emacs Lisp returns the new value rather than the cons, btw.

-- 
Johan Bockgård