Clojure is not javascript: scopes provided by functions aren't any
"better" than those created by let:

(let [start 1]
  (let [start 2]
    (println start))
  (println start))

prints 2 1 as you'd expect. You *can* write a macro that will expand
this way, and most of the time it's not a problem since clojure's
variables are immutable - mucking with the inner start won't affect
the outer start. However, the number of times you *should* do this
must be less than 1% of all macros, because there are subtle ways that
it can cause problems. The safety provided by ` fully qualifying
symbols is well worth typing an extra # every now and then.

If you still want to see how to do this, then (a) search recent Group
posts; someone asked this a few days ago, and (b) make sure you
understand why you're doing it.

Good luck, and enjoy Clojure!

On Nov 9, 12:46 am, Kenan Hneide <kenan.hne...@gmail.com> wrote:
> Hello,
>
> I am very new to Clojure, so please bear with me.  I was reading
> "Programming Clojure" and while reading about Macros, (specifically
> Symbol Capture) I had the following question, could it be possible to
> expand the macro into an anonymous function and evaluate that? For
> example:
>
> (defmacro bench [expr] `(let [start# (System/nanoTime)
> result# ~expr] {:result result# :elapsed (- (System/nanoTime)
> start#)}))
>
> would be expanded as :
>
> (macroexpand-1 '(bench f))
>
> (clojure.core/let [start__154__auto__ (System/nanoTime)
> result__155__auto__ f] {:result result__155__auto__, :elapsed
> (clojure.core/- (System/nanoTime) start__154__auto__)})
>
> instead, couldn't it be expanded to
>
> ((fn[f] (clojure.core/let [start (System/nanoTime) result f] {:result
> result, :elapsed (clojure.core/- (System/nanoTime) start)}))  f)
>
> My thinking is that the anonymous function would protect the scope of
> the let bindings inside of it which will make something like :
>
> (let [start 0] (bench f)) run correctly without capturing the start
> from the higher let binding.  I would like to know if this would work,
> and if not, why.  The only thing I can see this helping with is
> removing the need for "result#" syntax, and the compiler keeping track
> of unique symbols in the same scope
>
> Thanks,
> -Kenan

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