Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Please let me know. &OPTIONAL parameters.

22 views
Skip to first unread message

だいとうりょう むがべ

unread,
Nov 11, 2011, 12:05:13 AM11/11/11
to
I'm Japanese newbie. Excuse me in halting English.
Please let me know.
from CLHS. &OPTIONAL parameters.
http://www.lispworks.com/documentation/HyperSpec/Body/03_daf.htm

((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)))
=> (2 NIL 3 NIL NIL)
a is 2.
b is 3.
other nil.OK

((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6)
=> (6 T 3 NIL NIL)

T is the what?. From where?
"b" value Where?

Kaz Kylheku

unread,
Nov 11, 2011, 12:48:15 AM11/11/11
to
On 2011-11-11, だいとうりょう むがべ <mugabeda...@gmail.com> wrote:
> I'm Japanese newbie. Excuse me in halting English.
> Please let me know.
> from CLHS. &OPTIONAL parameters.
> http://www.lispworks.com/documentation/HyperSpec/Body/03_daf.htm
>
> ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)))
>=> (2 NIL 3 NIL NIL)
> a is 2.
> b is 3.
> other nil.OK

Hi,

I think you are mistaken here. A is 2, and C is 3; others NIL.

> ((lambda (&optional (a 2 b) (c 3 d) &rest x) (list a b c d x)) 6)
>=> (6 T 3 NIL NIL)
>
> T is the what?.

T stands for true. It is the value of B.

&optional (a 2 b) means this:

A : name of the paramter
2 : default value if argument is missing
B : name of a boolean parameter which indicates whether
the argument is present

B being T means "the caller passed an argument for A".

Think: what if the caller passes the value 2 for optional parameter A?
That is the same as the default value: ambiguous!

How can you tell if this 2 is the default 2, or the caller's 2?

The parameter B tells you. If it is T, then the argument is from
the caller. If it is NIL, then A's value was defaulted.

Cheers ...

Pascal J. Bourguignon

unread,
Nov 11, 2011, 12:58:42 AM11/11/11
to
(mapcar (lambda (args)
(format t "~%Call with ~S~%" args)
(apply (lambda (&optional (a 2 ap) (c 3 cp) &rest x)
(format t "The caller ~:[didn't~;did ~] provide an argument for A~%" ap)
(format t "The value bound to the parameter A is ~S~%" a)
(format t "The caller ~:[didn't~;did ~] provide an argument for C~%" cp)
(format t "The value bound to the parameter C is ~S~%" c)
(format t "The caller ~:[didn't provide any other argument~;did provide other arguments: ~:*~S~]~%" x)
(list a ap c cp x))
args))
'( ()
(2 3)
(10)
(10 11)
(10 11 12)
(10 11 12 13)))



Call with NIL
The caller didn't provide an argument for A
The value bound to the parameter A is 2
The caller didn't provide an argument for C
The value bound to the parameter C is 3
The caller didn't provide any other argument

Call with (2 3)
The caller did provide an argument for A
The value bound to the parameter A is 2
The caller did provide an argument for C
The value bound to the parameter C is 3
The caller didn't provide any other argument

Call with (10)
The caller did provide an argument for A
The value bound to the parameter A is 10
The caller didn't provide an argument for C
The value bound to the parameter C is 3
The caller didn't provide any other argument

Call with (10 11)
The caller did provide an argument for A
The value bound to the parameter A is 10
The caller did provide an argument for C
The value bound to the parameter C is 11
The caller didn't provide any other argument

Call with (10 11 12)
The caller did provide an argument for A
The value bound to the parameter A is 10
The caller did provide an argument for C
The value bound to the parameter C is 11
The caller did provide other arguments: (12)

Call with (10 11 12 13)
The caller did provide an argument for A
The value bound to the parameter A is 10
The caller did provide an argument for C
The value bound to the parameter C is 11
The caller did provide other arguments: (12 13)

--> ((2 NIL 3 NIL NIL)
(2 T 3 T NIL)
(10 T 3 NIL NIL)
(10 T 11 T NIL)
(10 T 11 T (12))
(10 T 11 T (12 13)))


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.
0 new messages