Why #' is used before lambda in Common Lisp? -


i know why common lisp code see has things like

(mapcar #'(lambda (x) (* x x)) '(1 2 3))

instead of just

(mapcar (lambda (x) (* x x)) '(1 2 3)),

which seems work well. beginning learn common lisp, , having background in scheme, intrigues me.

edit: know need #' function names because live in different namespace variables. question #' before lambda, lambda returns function object (i think). fact #'-less lambdas work because of macro expansion makes more intriguing...

#'foo abbreviation (function foo) reader.

in cl, there several different namespaces, #'foo or (function foo) return functional value of foo.

you may want search "lisp-1 vs. lisp-2", check other stackoverflow questions, or read old article py pitman , gabriel in order learn more concept of multiple namespaces (also called slots or cells of symbols).

the reason that, in case of lambda #' may omitted is, macro in cl, expands thusly (taken hyperspec):

(lambda lambda-list [[declaration* | documentation]] form*) ==  (function (lambda lambda-list [[declaration* | documentation]] form*)) ==  #'(lambda lambda-list [[declaration* | documentation]] form*) 

#' may still used historic reasons (i think in maclisp lambdas didn't expand function form), or because people think, "tagging" lambdas sharpquotes may make code more readable or coherent. there may special cases in makes difference, in general, doesn't matter form choose.

i guess can think of this: (function (lambda ...)) returns function (lambda ...) creates. note lambda in cl hyperspec has both a macro , symbol entry. latter:

a lambda expression list can used in place of function name in contexts denote function directly describing behavior rather indirectly referring name of established function.

from documentation of function:

if name lambda expression, lexical closure returned.

i think difference related calling lambda forms this: ((lambda ...) ...) treated form evaluated, vs. (funcall #'(lambda ...) ...). if want read more on topic, there c.l.l thread it.

some quotes thread:

(lambda (x) ... unquoted list structure. appearance argument function special form (function (lambda (x) ... causes function object exist

and:

it's compounded fact lambda macro rather late addition ansi common lisp, of old guys (i.e., me) learned lisp when needed supply #' lambda expression in mapping functions. otherwise non-existent lambda function invoked.

the macro addition changed that, of set in our ways want change.


Comments

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -