The best way to think ofFUNCTION
is as a special kind of quotation.
QUOTE
ing a symbol prevents it from being evaluated atall,
resulting in the symbol itself rather than the value of the variable named by that symbol.
来自http://www.gigamonkeys.com/book/functions.html
FUNCTION是种特殊的引用,引用一个符号就是阻止它被计算,
返回符号本身,而不是符号的值。
简便用法:
FUNCTION === #'
QUOTE === '
实践 FUNCTION: 打印函数曲线的有趣函数PLOT:
(defun plot (fn min max step)
(loop for i from min to max by step do
(loop repeat (funcall fn i) do (format t "*"))
(format t "~%")))
其中,fn是需要打印的函数,例如:
(defun pow (x) (* x x))
这样调用:(plot #'pow -6 6 1/2)
打印结果一个逆时针旋转的y = x * x图像:
************************************
*******************************
*************************
*********************
****************
*************
*********
*******
****
***
*
*
*
*
***
****
*******
*********
*************
****************
*********************
*************************
*******************************
************************************
也可以用lambda 把pow写成匿名函数的形式:
(plot #'(lambda (x) (* x x)) -6 6 1/2)
PS:(lambda (x) ...) 等价 #'(lambda (x) ...) ,因为lambda是一个特殊的关键字,
不应该被当作函数名来取值,也简化了
函数的书写,这神似于C语言中的函数名与函数名的指针等价,
如有雷同,纯属荣幸!
例如:
#include <stdio.h>
void foo() {
int x = 1;
}
void main() {
printf("test function and it's pointer.\n");
printf("foo = %d\n", (int)foo);
printf("&foo = %d\n", (int)&foo);
}
foo = 134513684
&foo = 134513684