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
From: Kent M Pitman
Subject: Re: Referencing an earlier argument in an ordinary lambda list ?
Date:
Message-ID: <uk5eckjqe.fsf@nhplace.com>
Didier Verna <······@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)
P� Tue, 19 Aug 2008 16:47:05 +0200, skrev Didier Verna
<······@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
On Aug 19, 12:23 pm, "John Thingstad" <·······@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?
P� Tue, 19 Aug 2008 20:22:42 +0200, skrev namekuseijin
<············@gmail.com>:
> On Aug 19, 12:23�pm, "John Thingstad" <·······@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?
oops.. 10 of cource!
--------------
John Thingstad
John Thingstad escribi�:
> P� Tue, 19 Aug 2008 16:47:05 +0200, skrev Didier Verna
> <······@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
From: Kenny
Subject: Re: Referencing an earlier argument in an ordinary lambda list ?
Date:
Message-ID: <48aaf2f2$0$29512$607ed4bc@cv.net>
Leandro Rios wrote:
> John Thingstad escribi�:
>
>> P� Tue, 19 Aug 2008 16:47:05 +0200, skrev Didier Verna
>> <······@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
From: Didier Verna
Subject: Re: Referencing an earlier argument in an ordinary lambda list ?
Date:
Message-ID: <muxabf9aq0c.fsf@uzeb.lrde.epita.fr>
"John Thingstad" <·······@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
From: Kenny
Subject: Re: Referencing an earlier argument in an ordinary lambda list ?
Date:
Message-ID: <48ab0e5e$0$29508$607ed4bc@cv.net>
Didier Verna wrote:
> "John Thingstad" <·······@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
In article <···············@uzeb.lrde.epita.fr>,
Didier Verna <······@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/
From: Didier Verna
Subject: Re: Referencing an earlier argument in an ordinary lambda list ?
Date:
Message-ID: <muxej4laq26.fsf@uzeb.lrde.epita.fr>
Rainer 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