18

Perhaps you can help me find this in the docs. I'm using pound-quote to be able to pass around unevaluated function names prior to execution. For example:

(#'cons 1 ())
;(1)

(defn funcrunner [func a b]
  (func a b))

(funcrunner cons 'a ())
;(a)

(funcrunner 'cons 'a ())
'()

(funcrunner #'cons 'a ())
;(a)

#'cons
;#'clojure.core/cons

(resolve (symbol 'cons))
;#'clojure.core/cons

My guess is that this is a reader macro.

My question is (a) What is the pound quote (#') shorthand for? (b) Can you explain what it is doing? (c) Can you locate it in the docs? (d) Is it actually shorthand for for resolve and symbol functions?

PS - For those not in the US - # is also known as a 'hash' or a 'cross-hash'.

PPS - I'm aware my example makes the need for this somewhat redundant. I'm interested to know if this is completely redundant or there are specific use cases.

Roly
  • 2,126
  • 2
  • 20
  • 34
hawkeye
  • 34,745
  • 30
  • 150
  • 304

2 Answers2

31

#' is a reader macro that expands to (var foo). What you're doing here is not passing around unevaluated functions, you're passing around vars which contain functions. The reason this works the way it does is because vars are functions that look up their contained value and call it:

user=> (defn foo [x] (+ x 10))
#'user/foo
user=> (#'foo 10)
20
user=> ((var foo) 10)
20

Notice that when I defined the function, a var was returned. It looks like what you've been doing! :)

Rayne
  • 31,473
  • 17
  • 86
  • 101
  • 2
    Anthony Grimes! I love what you've been doing in the Clojure community. It's an honour to have this question answered by you. – hawkeye Jun 08 '12 at 13:21
5

#' is the reader macro for var. See http://clojure.org/special_forms#var and http://clojure.org/vars

(var foo) returns the var named by the symbol foo, which can hold any kind of value, including functions.

Joost Diepenmaat
  • 17,633
  • 3
  • 44
  • 53