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



Pascal Costanza <pc@xxxxxxxxx> writes:

Pascal Bourguignon wrote:
jimka <jimka@xxxxxxxxx> writes:

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)))


This is dangerous.

What if the lambda list is:

(x &optional (y (delete-file "/some/important/file")))

to not write anything worse ?

You can call any function with (delete-file "/bla") as one of its
parameters. ?!?

The point is that you can put side effects in the lambda-list:

C/USER[32]> (defvar *x* 0)
*X*
C/USER[33]> (defun f (x &optional (y (incf *x*))) (values x y))
F
C/USER[34]> (f 1)
1 ;
1
C/USER[35]> (f 1)
1 ;
2
C/USER[36]> (f 1)
1 ;
3
C/USER[37]> *x*
3
C/USER[38]>


--
__Pascal Bourguignon__ http://www.informatimago.com/

"Klingon function calls do not have "parameters" -- they have
"arguments" and they ALWAYS WIN THEM."
.