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

The inverse of optional arguments.

12 views
Skip to first unread message

Jon Haugsand

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to

Maybe I am blind, but I cannot find out a way to do the following. I
have a function that have a function parameter where the latter is
called with some arguments. However I would like to call it with more
arguments if the function would accept them. An example:

(defun mydo (proc)
(apply proc (list :extra 5)))

(mydo #'(lambda (&key (extra 0)) (+ 4 extra)))
(mydo #'(lambda () 4))

The first call to MYDO works, however I would like something like the
second too, where APPLY simply should ignore extra parameters when not
required.

--
Jon Haugsand
Norwegian Computing Center, <http://www.nr.no/engelsk/>
<mailto:Jon.Ha...@nr.no> Pho: +47 22852608 / +47 22852500,
Fax: +47 22697660, Pb 114 Blindern, N-0314 OSLO, Norway

Raymond Wiker

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
Jon Haugsand <Jon.Ha...@nr.no> writes:

> Maybe I am blind, but I cannot find out a way to do the following. I
> have a function that have a function parameter where the latter is
> called with some arguments. However I would like to call it with more
> arguments if the function would accept them. An example:
>
> (defun mydo (proc)
> (apply proc (list :extra 5)))
>
> (mydo #'(lambda (&key (extra 0)) (+ 4 extra)))
> (mydo #'(lambda () 4))
>
> The first call to MYDO works, however I would like something like the
> second too, where APPLY simply should ignore extra parameters when not
> required.

&allow-extra-keys? Alternatively, use &rest with or without
&key/&allow-other-keys.

--
Raymond Wiker, Orion Systems AS
+47 370 61150

Erik Naggum

unread,
Feb 22, 2000, 3:00:00 AM2/22/00
to
* Jon Haugsand <Jon.Ha...@nr.no>

| Maybe I am blind, but I cannot find out a way to do the following. I
| have a function that has a function parameter where the latter is called

| with some arguments. However I would like to call it with more arguments
| if the function would accept them. An example:
|
| (defun mydo (proc)
| (apply proc (list :extra 5)))
|
| (mydo #'(lambda (&key (extra 0)) (+ 4 extra)))
| (mydo #'(lambda () 4))
|
| The first call to MYDO works, however I would like something like the
| second too, where APPLY simply should ignore extra parameters when not
| required.

first, you need to make sure that the function you're calling has &key in
its argument list. then, you call it with :allow-other-keys t in its
argument list. this will silence the default action for unknown keyword
arguments.

the function function-lambda-expression should return the lambda
expression for the function in question, but it is allowed to return nil
for any function, so you're a little out of luck without special support
for this thing. in Allegro CL, however, the function excl:arglist
returns the argument list of the function, as it was known when the
function was compiled (which may not be the verbatim argument list due to
macro "preprocessing"). this is usually sufficient to see whether you
have a keyword-argument-accepting function.

however, the general problem you're tring to solve is more interesting:
figuring out how to express a "protocol" for functions that accept
functions as arguments and call them with something other than a trivial
transformation of its own argument list. I believe this is partly what
makes up the concept of "interface" in Java and it would be kind of nice
if it were solved neatly for Common Lisp, too.

#:Erik

0 new messages