basically the following is the macro that creates a function from the
expression calculated using mathematica.. it seems to be doing something
still not the whole thing.. hopefully some of you may be able to help me.


(defmacro sexp2fn [sexp]
  (let [sym (eval sexp)
        vars (eval (math (Variables sexp)))]
    (println [sym vars])
    `(fn ~vars
        ~sym)))

;; when I macro expand the following

(sexp2fn (mathematica-eval "D[x*x+x*y,x]"))

;; I get the error whose stack trace is

Unable to resolve symbol: sexp in this context
  [Thrown class java.lang.Exception]

Restarts:
 0: [QUIT] Quit to the SLIME top level

Backtrace:
  0: clojure.lang.Compiler.resolveIn(Compiler.java:5677)


;; however if I replace the above macro with the following

(defmacro sexp2fn [sexp]
  (let [sym (eval sexp)
        vars []]           ;(eval (math (Variables sexp)))]
    (println [sym vars])
    `(fn ~vars
        ~sym)))

;and I macro expand the same expression i get
(clojure.core/fn [] (+ (* 2 x) y))

; as you can see the arguments list is empty .. i would have liked to have
[x y] over there and (math (Variables (+ (* 2 x) )y) would return [x y] but
for some reason sexp is not visible .. can anybody help?

does eval have any lexical scope in which it will run or does it run in the
null-lexical scope?
Sunil.

On Wed, Oct 13, 2010 at 2:27 PM, Meikel Brandmeyer <m...@kotka.de> wrote:

> Hi,
>
> On 13 Okt., 08:56, Laurent PETIT <laurent.pe...@gmail.com> wrote:
>
> > So in short: calling eval from the macro, on its argument.
>
> Beware the leopard!
>
> user=> (defn f
>         [constant]
>         `(+ x# (* ~constant y#)))
> #'user/f
> user=> (defn variables*
>         [form]
>         (if (seq? form)
>           (reduce into [] (map variables* form))
>           (when (and (symbol? form) (nil? (resolve form)))
>             [form])))
> #'user/variables*
> user=> (defmacro variables
>         [form]
>         `(variables* (quote ~form)))
> #'user/variables
> user=> (defmacro dynamic?-variables
>         [form]
>         `(variables ~(eval form)))
> #user/dynamic?-variables
> user=> (dynamic?-variables '(+ x (* 2 y)))
> [x y]
> user=> (dynamic?-variables (+ x (* 2 y)))
> java.lang.Exception: Unable to resolve symbol: x in this context
> (NO_SOURCE_FILE:18)
> user=> (dynamic?-variables (f 2))
> [x__12__auto__ y__13__auto__]
> user=> (let [x 2] (dynamic?-variables (f x)))
> java.lang.InstantiationException: user$eval48$eval49__50
> (NO_SOURCE_FILE:52)
>
> One has to be aware of the limitations of such an approach.
>
> Sincerely
> Meikel
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com<clojure%2bunsubscr...@googlegroups.com>
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
>

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to