I'm first starting to play with using key parameters in my own
functions, and I'm not entirely clear on the correct way to do it. I
have a simple "average" function:
(defun average (list &key key)
(if key
(floor (reduce #'+ list :key key) (length list))
(floor (reduce #'+ list) (length list))))
How do I deal with the fact that key parameters are supposed to be
optional, and just have it do the right thing if it's not there?
Thanks!
On Oct 19, 8:35 pm, Q <······@gmail.com> wrote:
> I'm first starting to play with using key parameters in my own
> functions, and I'm not entirely clear on the correct way to do it. I
> have a simple "average" function:
>
> (defun average (list &key key)
> (if key
> (floor (reduce #'+ list :key key) (length list))
> (floor (reduce #'+ list) (length list))))
>
> How do I deal with the fact that key parameters are supposed to be
> optional, and just have it do the right thing if it's not there?
>
> Thanks!
You can use cl:identity.
(defun average (list &key (key 'identity))
(floor (reduce #'+ list :key key) (lenght list)))
;;; warning! not tested
On Oct 19, 1:56 pm, KT <······@gmail.com> wrote:
> > How do I deal with the fact that key parameters are supposed to be
> > optional, and just have it do the right thing if it's not there?
>
> You can use cl:identity.
>
> (defun average (list &key (key 'identity))
> (floor (reduce #'+ list :key key) (lenght list)))
Or you can use the obvious:
(defun average (list &key key)
(floor (reduce #'+ list :key key) (length list)))
and Lisp will do the Right Thing. (&KEY parameters are NIL if not
supplied and REDUCE will handle a NIL KEY parameter properly.)
-Nathan
Q <······@gmail.com> writes:
> I'm first starting to play with using key parameters in my own
> functions, and I'm not entirely clear on the correct way to do it. I
> have a simple "average" function:
>
> (defun average (list &key key)
> (if key
> (floor (reduce #'+ list :key key) (length list))
> (floor (reduce #'+ list) (length list))))
>
> How do I deal with the fact that key parameters are supposed to be
> optional, and just have it do the right thing if it's not there?
In the case of REDUCE and other standard CL functions you don't have
to test for the presence of KEY. The functions handle a KEY of NIL to
mean that no key function is needed.
Zach
Zach Beane wrote:
> Q <······@gmail.com> writes:
>
>
>>I'm first starting to play with using key parameters in my own
>>functions, and I'm not entirely clear on the correct way to do it. I
>>have a simple "average" function:
>>
>>(defun average (list &key key)
>> (if key
>> (floor (reduce #'+ list :key key) (length list))
>> (floor (reduce #'+ list) (length list))))
>>
>>How do I deal with the fact that key parameters are supposed to be
>>optional, and just have it do the right thing if it's not there?
>
>
> In the case of REDUCE and other standard CL functions you don't have
> to test for the presence of KEY. The functions handle a KEY of NIL to
> mean that no key function is needed.
well live and learn. I knew there was a reason I still read this cesspool.
kenny
--
http://www.theoryyalgebra.com/
"Career highlights? I had two. I got an intentional walk
from Sandy Koufax and I got out of a rundown against the Mets."."
- Bob Uecker
On Oct 19, 8:38 pm, Ken Tilton <···········@optonline.net> wrote:
> well live and learn. I knew there was a reason I still read this cesspool.
Other than the frogs? Who could need one?
Tim Bradshaw wrote:
> On Oct 19, 8:38 pm, Ken Tilton <···········@optonline.net> wrote:
>
>
>
>>well live and learn. I knew there was a reason I still read this cesspool.
>
>
> Other than the frogs? Who could need one?
>
Indeed. I must say, he named himself well. It is hard to differentiate
his contributions in their diversity and range and sophistication from
these:
http://www.a1freesoundeffects.com/animals12554/frog.wav
kzo
--
http://www.theoryyalgebra.com/
"Career highlights? I had two. I got an intentional walk
from Sandy Koufax and I got out of a rundown against the Mets."."
- Bob Uecker
In article <························@v23g2000prn.googlegroups.com>,
Q <······@gmail.com> wrote:
> I'm first starting to play with using key parameters in my own
> functions, and I'm not entirely clear on the correct way to do it. I
> have a simple "average" function:
>
> (defun average (list &key key)
> (if key
> (floor (reduce #'+ list :key key) (length list))
> (floor (reduce #'+ list) (length list))))
>
> How do I deal with the fact that key parameters are supposed to be
> optional, and just have it do the right thing if it's not there?
>
> Thanks!
You can also check whether a keyword has been supplied
or not.
CL-USER> (defun foo (&key (bar 'baz bar-supplied-p))
(list bar bar-supplied-p))
FOO
CL-USER> (foo)
(BAZ NIL)
CL-USER> (foo :bar 'hello)
(HELLO T)