Re: programmatically determine if argument is list compatible to a given lambda list



jimka wrote:
I'd like to write a function which at run-time will determine whether
a given list
of arguments is compatible to a given lambda list.
For example.

(callable-as '(3) '(x))
==> return t because you CAN apply (lambda (x) nil) to '(3)

(callable-as '(3) '(x &rest y))
==> return t because you CAN apply (lambda (x &rest y) nil) to '(3)

(callable-as '(3) '(x y))
==> return nil because you CANNOT apply (lambda (x y) nil) to '(3)


Here is my attempt which fails miserably.

(defun callable-as (arg-list lambda-list)
(unwind-protect (apply `(lambda ,lambda-list
(return-from callable-as t))
arg-list)
(return-from callable-as nil)))

....but it's quite close:

(defun callable-as (arg-list lambda-list)
(unwind-protect (apply (coerce `(lambda ,lambda-list
(return-from callable-as t))
'function)
arg-list)
(return-from callable-as nil)))

[untested]

Pascal

--
My website: http://p-cos.net
Common Lisp Document Repository: http://cdr.eurolisp.org
Closer to MOP & ContextL: http://common-lisp.net/project/closer/
.



Relevant Pages