Hi Andrew,

the approach suggested by Tassilo looks correct.
Macros are evaluated at compile-time, so you need a termination
condition that can be decided without actually running your form. In
you're case you want run-time delayed evaluation, which is easily
achieved by wrapping the expression into a closure.

On Dec 3, 6:45 pm, Andrew <ache...@gmail.com> wrote:
> Aren't the calls to itry-the-fn different from the calls to itry-the-macro?
> For example, let's say my expr is (/ a b) where b is currently zero and
> maybe the user decides to set a new value for b when prompted.
>
> itry-the-macro can be called this way: (itry (/ a b)) and is able to print
> out the source code (/ a b) for the user as a string if it needs to.
>
> itry-the-fn has to be called this way, right? (itry (fn [] (/ a b)) and is
> unable to print out the source code of the expression. Let me know if I'm
> mistaken.
You are right about the calling interface. itry-the-fn needs to have
its argument delayed explicitly, i.e. (itry (fn [] (/ a b))), and
cannot look into the form. A standard way to solve this problem is to
define both, a function and a macro which provides a more convenient
interface to the function.

(defmacro itry-the-macro [expr]
   `(itry-the-fn (fn [] ~expr)))
or in case you want to preserve access to the source code of the
expression:
(defmacro itry-the-macro [expr]
   `(itry-the-fn (fn [] ~expr)
                    '~expr)))
and itry-the-fn takes two arguments, one of which it can call and one
that can be expected, printed etc.

This approach of defining a macro as a shallow wrapper around a
function is quite common. It is also used in a couple of places in
clojure.core

Hope your attic lights up a little,

   Nils

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