quote - What's the difference between ' and #' in Lisp? -


it seems both

(mapcar 'car '((foo bar) (foo1 bar1)))  

and

(mapcar #'car '((foo bar) (foo1 bar1))) 

work same.

and know ' means (quote symbol) , #' means (function function-name).

but what's underlying difference? why these 2 both work in previous mapcar?

'foo 

evaluates symbol foo.

#'foo 

evaluates function bound name foo.

in lisp symbol can called function when symbol foo has function binding. here car symbol has function binding.

but not work:

(flet ((foo (a) (+ 42)))   (mapcar 'foo '(1 2 3 4 5))) 

that's because foo symbol not access local lexical function , lisp system complain when foo not function defined elsewhere.

we need write:

(flet ((foo (a) (+ 42)))   (mapcar #'foo '(1 2 3 4 5))) 

here (function foo) or shorthand notation #'foo refers lexical local function foo.

note in

(funcall #'foo ...) 

vs.

(funcall 'foo ...) 

the later might 1 more indirection, since needs lookup function symbol, while #'foo denotes function directly.

summary:

if symbol has function binding, calling function through symbol works.


Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -