[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

create and evaluate function dynamically



>Date: Tue, 31 Mar 1992 11:07:10 +0100
>To: info-mcl@cambridge.apple.com
>From: lafourca@imag.fr
>Subject: create and evaluate function dynamically
>
>I want to create and evaluate a function dynamically without calling the
>eval function (I would like to use funcall or apply). I have a solution
>with eval, but I suppose that it could be done more nicely.
>

The problem is that it's not very nice to have special variables all over 
the place. Your (action state) functions presumably look like this:

   (lambda nil  
       (if (foo var1) var2 var3))

And you are getting "Undeclared free variable" warnings on var1, var2, etc.
One solution is to change the macro which creates these lambdas so that
these variables get passed explicitly. Then, (action state) looks like this:

   (lambda (var1 var2 var3)  
       (if (foo var1) var2 var3)))

Now you won't get any warnings, and you'll find it much easier to debug too!

Fortunately, your code already saves all the arguments in (arguments state), so 
you know which ones you need to APPLY.

Here's what your new STATE-RUN might look like:

(defmethod state-run ((self state-class))
  (let ((function  (action self))        ; (action self) is a lambda expression
        (arg-names (arguments self)))    ; arg list for (action self)

    (if (not (functionp function))        ; no action (NIL)
      nil
      (let ((arg-values (automata-feature-values (owner self) arg-names)))      
        (apply function arg-values)
                 ; save the env, this function works
        (save-environment self
                          arg-names
                          arg-values)))))