Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>#(foo %)</code> is just reader shorthand for <code>(fn [arg] (foo arg))</code>. There's no reason to write a macro that expands into <code>#(...)</code>. All the <code>%</code>'s in a <code>#(...)</code> construct are expanded into gensyms right away anyways.</p> <pre><code>user&gt; `#(foo % %1 %2) (fn* [user/p1__1877 user/p2__1878] (user/foo user/p1__1877 user/p1__1877 user/p2__1878)) </code></pre> <p>If you're ever writing a macro that expands to build anonymous functions, you might as well just expand them to <code>fn</code> forms yourself. In your case, you should probably just use <code>fn</code> directly and skip the macros. <code>fn</code> is Clojure's <code>lambda</code>.</p> <p>The difference between <code>(fn [] ...)</code> and <code>(lambda () ...)</code> in this case is that "fn" is shorter to type than "lambda", and <code>fn</code> takes a vector for its bindings whereas <code>lambda</code> takes a list. If you're using Clojure, you will have to get used to this eventually, because vectors are always used for collections of bindings, in all the <code>do</code> forms and in <code>for</code> and <code>binding</code> etc. The rationale behind this as I understand it is that lists are used for function calls or macro calls, and vectors are used for things that aren't calls (lists of symbols to bind, for example). Arguably, it makes it easier to scan the code visually than lists-all-the-way-down. Clojure is not Common Lisp, and you will experience pain if you try to force it to be.</p> <p>If you really, really wanted to do this, just to say you did:</p> <pre><code>user&gt; (defmacro lambda [args &amp; body] `(fn ~(vec args) ~@body)) user&gt; ((lambda (x) (println x)) "foo") foo nil </code></pre> <p>This doesn't let you put a docstring or metadata on your function, among other things. I don't think you would want to use this in a real Clojure program.</p>
 

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