Christophe Grand a écrit :
> You also can do something like this:
>
> (defmacro let-eval [vars expr]
>   (let [bindings (mapcat #(list (list `quote %) %)  vars)]
>     `(eval (list 'let [...@bindings] ~expr))))
>   

Hmm... not quite:
user=> (let [x [2 3]] (let-eval [x] '(rest x)))
(3)
user=> (let [x '(2 3)] (let-eval [x] '(rest x)))
java.lang.ClassCastException: java.lang.Integer cannot be cast to 
clojure.lang.IFn (NO_SOURCE_FILE:0)

The following code fixes this bug:
(defmacro let-eval [vars expr]
  (let [bindings (mapcat #(list (list `quote %) (list `list ``quote %))  
vars)]
    `(eval (list 'let [...@bindings] ~expr))))
user=> (let [x '(2 3)] (let-eval [x] '(rest x)))
(3)
user=> (let [x 12] (let-eval [x] '(+ x 15)))
27


But maybe this is better:
(defmacro let-eval [bindings expr]
  (let [binding-forms (map #(list `quote %) (take-nth 2 bindings))
        expr-forms (map #(list `list ``quote %) (take-nth 2 (rest 
bindings)))]
    `(eval (list 'let [~@(interleave binding-forms expr-forms)] ~expr))))
user=> (let-eval [x 12] '(+ x 15))
27
user=> (let-eval [x '(2 3)] '(rest x))
(3)
user=> (let-eval [[x y] [2 3]] '(+ x y))
5

Christophe

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to