The best way to think of FUNCTION
is as a special
kind of quotation.
QUOTE
ing a symbol prevents it from being evaluated at all,
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