From: Q
Subject: Using key parameters
Date: 
Message-ID: <1192815312.147065.270120@v23g2000prn.googlegroups.com>
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!

From: KT
Subject: Re: Using key parameters
Date: 
Message-ID: <1192816615.559954.166380@e9g2000prf.googlegroups.com>
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
From: Nathan Froyd
Subject: Re: Using key parameters
Date: 
Message-ID: <1192819539.419885.99430@v29g2000prd.googlegroups.com>
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
From: Zach Beane
Subject: Re: Using key parameters
Date: 
Message-ID: <m3bqavndwl.fsf@unnamed.xach.com>
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
From: Ken Tilton
Subject: Re: Using key parameters
Date: 
Message-ID: <ZI7Si.60$aK3.9@newsfe12.lga>
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
From: Tim Bradshaw
Subject: Re: Using key parameters
Date: 
Message-ID: <1192828565.413940.189220@v29g2000prd.googlegroups.com>
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?
From: Ken Tilton
Subject: Re: Using key parameters
Date: 
Message-ID: <VgcSi.359$aK3.214@newsfe12.lga>
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
From: Rainer Joswig
Subject: Re: Using key parameters
Date: 
Message-ID: <joswig-29E05B.21355419102007@news-europe.giganews.com>
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)