title: Clojure: create function whose name is given as a parameter at run-time
body: <p>I'm trying to create a Clojure function, that returns another function with a custom name. My attempts so far:</p>
  
  <pre><code>(defn function-with-custom-name [name] (fn name [] 42))
  (function-with-custom-name "hello")
  # --&gt; #object[lang.main$function_with_custom_name$name__4660 0xa6afefa "lang.main$function_with_custom_name$name__4660@a6afefa"]
  # syntactically ok, but its name is 'name' and not 'hello'
  
  (defn function-with-custom-name [name] (fn (symbol name) [] 42))
  # --&gt; CompilerException java.lang.IllegalArgumentException: Parameter declaration symbol should be a vector, compiling:(/tmp/form-init3365336074265515078.clj:1:40)
  
  (defn function-with-custom-name [name] (fn '(symbol name) [] 42))
  # --&gt; CompilerException java.lang.IllegalArgumentException: Parameter declaration quote should be a vector, compiling:(/tmp/form-init3365336074265515078.clj:1:40)
  </code></pre>
  
  <p>I understand that <code>fn</code> is a macro, and therefore proper quoting is probably important for the parameter, but as per above, I could not get it right, but I'm 99% sure there is a way, since (looking at the source of <code>fn</code>), the only criteria is that the first parameter should be recognized as a symbol.</p>
  
  <p>Any hints on how to do this?</p>
  
  <p><strong>EDIT</strong>: Use-case, as asked in the comment: I'm writing a simple language interpreter in Clojure, which (among other things) lets you create functions. The functions from my language are currently represented by anonymous Clojure functions. However, it would make debugging the interpreter much easier, if the Clojure functions also did have a name.</p>
  
  <p><strong>EDIT2</strong>: The first edit made me think, and I came to the conclusion that I cannot use macro-based solutions for this problem, since I need to create the functions run-time (and, as far as I remember, macros can only work at compile-time). --> Changed the question title for clarity. Still, please don't delete the macro-based answers, since they give helpful insight.</p>