我想存储一个像这样的函数print
在一个变量中,这样我就可以输入一些简短的内容,例如p
, e.g:
In 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
above:
*** - EVAL: undefined function P
而这与Attempt 2
and 3
in 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.
So:
- 什么是
try using a symbol
mean? p
绝对是一个symbol
;假阳性?
- 怎么了
eval
?评价不就是p
产生程序print
?
- 我想
Lisp
程序是first class objects
。为什么是Attempt 1
不工作像Scheme
?
EDIT
(从下面的评论移过来)
我想知道为什么(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)
yet defvar
and setf
两者都为变量赋值。
相关的错误是:
*** - DEFVAR: non-symbol (SYMBOL-FUNCTION 'P) cannot be a variable ;; Clisp
Error: (SYMBOL-FUNCTION (QUOTE P)) is not of type SYMBOL. ;; Gcl