Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Everything in Clojure is fully dynamic, but you have to take note of when you're working with a <em>Var</em> and when you're working with the <em>Function</em> which is the current <em>value</em> of that Var.</p> <p>In your first example:</p> <pre><code>(defn factorial [n] (if (&lt; n 2) n (* n (factorial (dec n))))) </code></pre> <p>The <code>factorial</code> symbol is resolved to the Var <code>#'user/factorial</code>, which is then evaluated by the compiler to get its current value, a compiled function. This evaluation happens only once, when the function is compiled. The <code>factorial</code> in this first example is the <em>value</em> of the Var <code>#'user/factorial</code> at the moment the function was defined.</p> <p>In the second example:</p> <pre><code>(defn factorial [n] (if (&lt; n 2) n (* n (#'factorial (dec n))))) </code></pre> <p>You have explicitly asked for the Var <code>#'user/factorial</code>. Invoking a Var has the same effect as dereferencing the Var and invoking its (function) value. This example could be written more explicitly as:</p> <pre><code>(defn factorial [n] (if (&lt; n 2) n (* n ((deref (var factorial)) (dec n))))) </code></pre> <p>The <code>clojure.contrib.trace/dotrace</code> macro (which I wrote, ages ago) uses <code>binding</code> to temporarily rebind a Var to a different value. It does not change the definition of any functions. Instead, it creates a new function which calls the original function and prints the trace lines, then it binds that function to the Var.</p> <p>In your first example, since the original function was compiled with the <em>value</em> of the <code>factorial</code> function, <code>dotrace</code> has no effect. In the second example, each invocation of the <code>factorial</code> function looks up the current value of the <code>#'user/factorial</code> Var, so each invocation sees the alternate binding created by <code>dotrace</code>. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload