common lisp中的funcall

common lisp:
http://www.gigamonkeys.com/book/
http://acl.readthedocs.org/en/latest/zhCN/

CL-USER> (remove-if-not #'evenp '(1 2 3 4 5 6 7 8 9 10))
(2 4 6 8 10)

In this case, the predicate is the function EVENP, which returns true if its argument is an even number.
The funny notation #' is shorthand for "Get me the function with the following name."
Without the #', Lisp would treat evenp as the name of a variable and look up the value of the variable, not the function.

Common Lisp provides two functions for invoking a function through a function object: FUNCALL and APPLY.
Like FUNCALL, the first argument to APPLY is a function object. But after the function object, instead of individual arguments, it expects a list.
(defun foo(a b c) (list a b c))
(foo 1 2 3)

(funcall #'foo 1 2 3)
(apply #'foo (list 1 2 3))
(apply #'foo 1 (list 2 3))
(apply #'foo 1 2 (list 3))

CL-USER> (let ((count 0)) #'(lambda () (setf count (1+ count))))
#

(defparameter *fn* (let ((count 0)) #'(lambda () (setf count (1+ count)))))
CL-USER> (funcall *fn*)
1
CL-USER> (funcall *fn*)
2
CL-USER> (funcall *fn*)
3

each macro defines its own syntax, determining how the s-expressions it's passed are turned into Lisp forms. With macros as part of the core language it's possible to build new syntax--control constructs such as WHEN, DOLIST, and LOOP as well as definitional forms such as DEFUN and DEFPARAMETER--as part of the "standard library" rather than having to hardwire them into the core. This has implications for how the language itself is implemented, but as a Lisp programmer you'll care more that it gives you another way to extend the language, making it a better language for expressing solutions to your particular programming problems.

the job of a macro isn't to do anything directly--its job is to generate code that will later do what you want.

我们可以用 ' 作为 quote(...) 的缩写,也可以用 #' 作为 function(...) 的缩写:
>(defun plus(x y) (+ x y))
PLUS
>(plus 1 4)
5
>(funcall #'plus 1 4)
5
>(funcall (function plus) 1 4)
5
>(apply #'plus (list 1 4))
5
>(apply (function plus) (list 1 4))
5
>#'plus
#
>#'+
#
>(function plus)
#
>(function +)
#

((x) (+ x 100)) and
(lambda (x) (+ x 100)) is same
but we used to use lambda to express anonymous function
lambda表达式本身就是一个函数

你可能感兴趣的:(计算机相关)