[pro] why :key arguments?

Tamas Papp tkpapp at gmail.com
Mon Jul 4 09:31:31 UTC 2011


Why do some CL library functions have :key arguments?

I am asking because I am working on some statistics functions, and the 
design choice came up.  Specifically, I can write functions like

(defun quantiles (sequence quantiles &key (key #'identity))
   ...)

but it is a bit cumbersome.  I can make my code simpler by relying on 
calls like

(quantiles (map 'vector key vector) quantiles)

but this conses a bit more.  Not a major concern at the moment, but if it 
ever becomes one, I could just define a compiler macro to take care of 
forms like this and transform to something like

(quantile-with-keys vector quantiles key)

I also thought of defining something like

(defgeneric map1 (function object)
  (:method (function (list list))
     (mapcar function list))
  ...)

for the sole purpose of mapping objects to similar objects (eg lists to 
lists, arrays to arrays, etc) and allow it to be optimized away (like 
above) by compiler macros.

I would appreciate advice on this.  I am especially interested in the 
reason why some CL functions have :key arguments: is it because of 
efficiency, backward-compatibility/history, or something else?

Thanks,

Tamas





More information about the pro mailing list