我想print在变量中存储一个函数,这样我就可以输入一些简短的东西p,例如:
在Scheme:
(define print display)
(print "Hello world\n")
;; alternate way
(define print 'display)
((eval print) "Hello world\n")
同样的方法似乎不适用于Common Lisp:
(defvar p 'print)
;;(print (type-of p))
(p "Hello world") ;; Attempt 1
((eval p) "Hello world") ;; >> Attempt 2
((eval (environment) p) "Hello world") ;; Attempt 3
我在Attempt 1上面遇到了这个错误:
*** - EVAL: undefined function P
这与Attempt 2和3在Clisp:
*** - EVAL: (EVAL (ENVIRONMENT) P) is not a function name; try using a
symbol instead
*** - EVAL: (EVAL P) is not a function name; try using a symbol instead
并与gcl:
Error: (EVAL P) is invalid as a function.
Error: (EVAL (ENVIRONMENT) P) is invalid as a function.
所以:
- 是什么
try using a symbol意思?p绝对是一个symbol;假阳性? - 怎么了
eval?评估不p产生程序print吗? - 我以为
Lisp程序是first class objects. 为什么Attempt 1不像在工作Scheme?
编辑
(从下面的评论移动)
我想知道为什么(setf (symbol-function 'p) #'print)不能这样工作
(setf (symbol-function 'p) 'print)。我收到以下(不是很有帮助)错误:
*** - SYSTEM::%PUTD: PRINT is not a function ;; CLisp
Error: PRINT is not of type LIST. ;; Gcl
我知道尖号(#)应该消除函数和
同名变量之间的歧义,但在这种情况下,只有一个print函数。
另外,为什么它不能使用defvar而不是setf像这样:
(defvar (symbol-function 'p) #'print)
然而defvar,setf两者都为变量赋值。
相关的错误是:
*** - DEFVAR: non-symbol (SYMBOL-FUNCTION 'P) cannot be a variable ;; Clisp
Error: (SYMBOL-FUNCTION (QUOTE P)) is not of type SYMBOL. ;; Gcl