Referencing an earlier argument in an ordinary lambda list ?

This is a discussion on Referencing an earlier argument in an ordinary lambda list ? within the lisp forums in Programming Languages category; Hi, is it okay to have the default value of an optional argument reference a former argument? Such as in: (defun test (foo &optional (bar (+ foo 10))) (list foo bar)) Thanks. -- Resistance is futile. You will be jazzimilated. Scientific site: http://www.lrde.epita.fr/~didier Music (Jazz) site: http://www.didierverna.com EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bicêtre, France Tel. +33 (0)1 44 08 01 85 Fax. +33 (0)1 53 14 59 22...

Go Back   Application Development Forum > Programming Languages > lisp

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-19-2008, 10:47 AM
Didier Verna
Guest
 
Default Referencing an earlier argument in an ordinary lambda list ?


Hi,

is it okay to have the default value of an optional argument reference a
former argument? Such as in:

(defun test (foo &optional (bar (+ foo 10)))
(list foo bar))

Thanks.

--
Resistance is futile. You will be jazzimilated.

Scientific site: http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bicêtre, France
Tel. +33 (0)1 44 08 01 85 Fax. +33 (0)1 53 14 59 22
Reply With Quote
  #2  
Old 08-19-2008, 11:23 AM
John Thingstad
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

På Tue, 19 Aug 2008 16:47:05 +0200, skrev Didier Verna
<didier@lrde.epita.fr>:

> Hi,
>
> is it okay to have the default value of an optional argument reference a
> former argument? Such as in:
>
> (defun test (foo &optional (bar (+ foo 10)))
> (list foo bar))
>
> Thanks.
>


This is weird but should work.
Arguments are evaluated left to right so 'foo is guarantied to be assigned
a value before 'bar is evaluated.
The same is not true in Scheme which does not specify the order of
argument evaluation.
So it works like let*

Also note
(defun test (&aux (a 10) (b (+ a 10)) (c (+ b 10)))
(list a b c))

returns: (12 20 30)

--------------
John Thingstad
Reply With Quote
  #3  
Old 08-19-2008, 12:03 PM
Leandro Rios
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

John Thingstad escribió:
> På Tue, 19 Aug 2008 16:47:05 +0200, skrev Didier Verna
> <didier@lrde.epita.fr>:
>
>> Hi,
>>
>> is it okay to have the default value of an optional argument reference a
>> former argument? Such as in:
>>
>> (defun test (foo &optional (bar (+ foo 10)))
>> (list foo bar))
>>
>> Thanks.
>>

>
> This is weird but should work.
> Arguments are evaluated left to right so 'foo is guarantied to be
> assigned a value before 'bar is evaluated.
> The same is not true in Scheme which does not specify the order of
> argument evaluation.
> So it works like let*
>
> Also note
> (defun test (&aux (a 10) (b (+ a 10)) (c (+ b 10)))
> (list a b c))
>
> returns: (12 20 30)
>
> --------------
> John Thingstad


Wouldn't this mean that lambda behaves like let*? I have the
understanding that the order of evaluation is left to right, but that
the bindings were established in parallel. I fail to find any mention of
this in the CLHS, so any pointers will be greatly appreciated.

Leandro


Reply With Quote
  #4  
Old 08-19-2008, 12:19 PM
Kenny
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

Leandro Rios wrote:
> John Thingstad escribió:
>
>> På Tue, 19 Aug 2008 16:47:05 +0200, skrev Didier Verna
>> <didier@lrde.epita.fr>:
>>
>>> Hi,
>>>
>>> is it okay to have the default value of an optional argument reference a
>>> former argument? Such as in:
>>>
>>> (defun test (foo &optional (bar (+ foo 10)))
>>> (list foo bar))
>>>
>>> Thanks.
>>>

>>
>> This is weird but should work.
>> Arguments are evaluated left to right so 'foo is guarantied to be
>> assigned a value before 'bar is evaluated.
>> The same is not true in Scheme which does not specify the order of
>> argument evaluation.
>> So it works like let*
>>
>> Also note
>> (defun test (&aux (a 10) (b (+ a 10)) (c (+ b 10)))
>> (list a b c))
>>
>> returns: (12 20 30)
>>
>> --------------
>> John Thingstad

>
>
> Wouldn't this mean that lambda behaves like let*?


Yes, it does.

I have the
> understanding that the order of evaluation is left to right, but that
> the bindings were established in parallel. I fail to find any mention of
> this in the CLHS, so any pointers will be greatly appreciated.


Sorry, I can never find anything in the CLHS. It is at once the most
massively cross-referenced and the most unlookable document known to
mankind, kinda the Where's Waldo? of language references.

kt
Reply With Quote
  #5  
Old 08-19-2008, 12:50 PM
Rainer Joswig
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

In article <muxr68lawpi.fsf@uzeb.lrde.epita.fr>,
Didier Verna <didier@lrde.epita.fr> wrote:

> Hi,
>
> is it okay to have the default value of an optional argument reference a
> former argument? Such as in:
>
> (defun test (foo &optional (bar (+ foo 10)))
> (list foo bar))
>
> Thanks.


The CLHS says:

3.4.1 Ordinary Lambda Lists

....

An init-form can be any form. Whenever any init-form is
evaluated for any parameter specifier, that form may refer
to any parameter variable to the left of the specifier in which
the init-form appears, including any supplied-p-parameter
variables, and may rely on the fact that no other parameter
variable has yet been bound (including its own parameter variable).

....

--
http://lispm.dyndns.org/
Reply With Quote
  #6  
Old 08-19-2008, 01:10 PM
Didier Verna
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

Rainer Joswig <joswig@lisp.de> wrote:

> The CLHS says:
>
> 3.4.1 Ordinary Lambda Lists
>
> ...
>
> An init-form can be any form. Whenever any init-form is
> evaluated for any parameter specifier, that form may refer
> to any parameter variable to the left of the specifier in which
> the init-form appears, including any supplied-p-parameter
> variables, and may rely on the fact that no other parameter
> variable has yet been bound (including its own parameter variable).


Gosh. It seems that even when I read that CLHS thing, I keep missing
the interesting bits :-/

Sorry for the noise.

--
Resistance is futile. You will be jazzimilated.

Scientific site: http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bicêtre, France
Tel. +33 (0)1 44 08 01 85 Fax. +33 (0)1 53 14 59 22
Reply With Quote
  #7  
Old 08-19-2008, 01:11 PM
Didier Verna
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

"John Thingstad" <jpthing@online.no> wrote:

> This is weird but should work.


Is it? I find that it saves me some supplied-p parameters from time to
time, not to mention a better code readability.

--
Resistance is futile. You will be jazzimilated.

Scientific site: http://www.lrde.epita.fr/~didier
Music (Jazz) site: http://www.didierverna.com

EPITA/LRDE, 14-16 rue Voltaire, 94276 Le Kremlin-Bicêtre, France
Tel. +33 (0)1 44 08 01 85 Fax. +33 (0)1 53 14 59 22
Reply With Quote
  #8  
Old 08-19-2008, 01:17 PM
Kent M Pitman
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

Didier Verna <didier@lrde.epita.fr> writes:

> is it okay to have the default value of an optional argument reference a
> former argument? Such as in:
>
> (defun test (foo &optional (bar (+ foo 10)))
> (list foo bar))


Yes, it's most definitely an intentional part of the design that you
should be able to do this.

For example, it accommodates arguments that depend on other arguments, as in:

(defun foo (list &optional (size (length list)))
;; A more serious function body would go here,
;; possibly calling FOO recursively with (cdr list) and (1- size),
;; avoiding the need to call LENGTH on each recursive call.
(list list size))

(foo '(a b c)) => ((A B C) 3)
(foo '(a b c) 2) => ((A B C) 2)

(A related possibility would be to have the argument default to the class or
type of an argument.)

It also accommodates arguments that might often want to be defaulted like
others:

(defun foo (&optional (from 0) (to from))
(list from to))

(foo) => (0 0)
(foo 5) => (5 5)
(foo 5 7) => (5 7)

Note, too, that you can also depend on the presentp arguments to the left.

(defun foo (&optional (x nil x-p) (y (when x-p :mismatched)))
(list x y))

(foo) => (NIL NIL)
(foo nil) => (NIL :MISMATCHED)
(foo nil nil) => (NIL NIL)

Reply With Quote
  #9  
Old 08-19-2008, 02:16 PM
Kenny
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

Didier Verna wrote:
> "John Thingstad" <jpthing@online.no> wrote:
>
>
>>This is weird but should work.

>
>
> Is it? I find that it saves me some supplied-p parameters from time to
> time, not to mention a better code readability.
>


Yeah, it is weird JT thought it was weird. Of course one would derive a
default for an input to a function from other inputs. I would go batsh*t
if it were otherwise.

my2.

kzo
Reply With Quote
  #10  
Old 08-19-2008, 02:22 PM
namekuseijin
Guest
 
Default Re: Referencing an earlier argument in an ordinary lambda list ?

On Aug 19, 12:23*pm, "John Thingstad" <jpth...@online.no> wrote:
> Also note
> (defun test (&aux (a 10) (b (+ a 10)) (c (+ b 10)))
> * * * * * * * *(list a b c))
>
> returns: (12 20 30)


Is that 12 right? Why would a go to 12?
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 06:14 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.