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



jimka wrote:
if i try that in sbcl, this is what happens. (callable-as '(3) '(x
y))

invalid number of arguments: 1
[Condition of type SB-INT:SIMPLE-PROGRAM-ERROR]

Restarts:
0: [ABORT-REQUEST] Abort handling SLIME request.
1: [TERMINATE-THREAD] Terminate this thread (#<THREAD "repl-
thread" {10010910F1}>)

Backtrace:
0: ((LAMBDA (X Y)) #<unavailable argument>)
1: (CALLABLE-AS (3) (X Y))
2: (SB-INT:EVAL-IN-LEXENV (CALLABLE-AS (QUOTE (3)) (QUOTE (X Y)))
#<NULL-LEXENV>)
3: (SWANK::EVAL-REGION "(callable-as '(3) '(x y))
" T)

Ah, of course. You should use handler-case, not unwind-protect:

(defun callable-as (arg-list lambda-list)
(handler-case (apply (coerce `(lambda ,lambda-list
(return-from callable-as t))
'function)
arg-list)
(program-error () nil)))

You should probably also make sure that the safety settings are correct at the time when coerce is executed. (See 3.5.1.1 in the HyperSpec.)

Finally, the lambda expression is embedded in the null lexical environment, so it doesn't see the 'callable-as block. But it should be good enough to just return 't from the lambda expression - you don't need to be specific about where to return from.

Finally, it might make sense to consider a macro version - if the lambda lists are quoted constants all of the time, a macro can be more efficient. (Or you could define a compiler macro.)


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

  • Re: Is this a appropriate use of closures?
    ... > involves an expansion of the lambda MACRO. ... but that lambda expression is primitively understood--no functional ... > and quote is a special-form, ...
    (comp.lang.lisp)
  • Re: # and lambda
    ... You can think of FUNCTION to be a special operator to look up functions ... So a lambda expression is a name-like thing for functions without ... is the #' reader macro to make it a bit shorter. ...
    (comp.lang.lisp)
  • Re: spec 3.1.2.1.2 and the lambda exception
    ... > I believe a form with LAMBDA in the CAR position ... > is just a macro call, not a real lambda expression. ... > macro will expand to a special form with a lambda expression inside. ...
    (comp.lang.lisp)
  • Re: programmatically determine if argument is list compatible to a given lambda list
    ... jimka wrote: ... of arguments is compatible to a given lambda list. ... (defun callable-as (arg-list lambda-list) ...
    (comp.lang.lisp)
  • Re: Macros in the car of an application
    ... > try to write a macro that would allow things like that. ... > a macro I called flambda. ... Note that your macro could assume that all of the lambda variables are ... symbols by duplicating the value bindings into function bindings. ...
    (comp.lang.lisp)