如何在Lisp中的变量中存储函数并使用它 [英] How to store a function in a variable in Lisp and use it

查看:148
本文介绍了如何在Lisp中的变量中存储函数并使用它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在变量中存储像 print 这样的函数,这样我就可以像 p 这样简短地输入内容,例如:

方案中:

  (定义打印显示)
(打印 Hello world\n)
;;替代方式
(定义打印'显示)
((等效打印) Hello world\n)

通用Lisp 中似乎不起作用:

 (defvar p'print)
;;(print(p的类型))
(p Hello world);;尝试1
((eval p) Hello world);; >>尝试2
((eval(environment)p) Hello world);;尝试3

我遇到尝试1

  ***-EVAL:未定义函数P 

这与尝试2 3 Clisp 中:

  ***-评估:(评估(环境)P)不是函数名称;尝试使用
符号代替
***-EVAL:(EVAL P)不是函数名;尝试使用符号代替

并使用 gcl

 错误:(EVAL P)作为函数无效。 
错误:(EVAL(环境)P)作为函数无效。

所以:




  • 尝试使用符号是什么意思? p 绝对是符号;假阳性?

  • eval 怎么了?对 p 的求值不是产生 print 的过程吗?

  • I认为 Lisp 过程是头等对象。为什么尝试1 不能像 Scheme 那样工作?



编辑

(从下面的评论中删除)



我想知道为什么(setf(symbol-function'p)#'print)不能这样工作

(setf(symbol-函数'p)'打印)。我收到以下(不太有用)错误:

  ***-SYSTEM ::%PUTD:PRINT不是函数;; CLisp 
错误:PRINT的类型不是LIST。 ;; Gcl

我知道尖锐的符号()应该在函数和具有相同名称的变量

之间进行歧义消除,但是在这种情况下,只有一个 print 打印函数。 / p>

此外,为什么它不能与 defvar 而不是 setf 像这样:

 (defvar(symbol-function'p)#'print)

defvar setf 都将值赋给变量。

相关的错误是:

  ***-DEFVAR :非符号(SYMBOL-FUNCTION'P)不能为变量;; Clisp 
错误:(SYMBOL-FUNCTION(QUOTE P))不是SYMBOL类型。 ;; Gcl


解决方案

Common Lisp是 Lisp-2 。其中,在函数名称空间中评估函数调用中的第一个位置。在您的情况下,符号 p 表示变量,而不是函数。



效果更好:

 (defvar p'print)

(funcall p Hello world)

或者可能,但是您可能不想这样做:

 (setf(symbol-function'p)#'print)

(p Hello world )


I want to store a function like print in a variable so that I can just type something short like p, e.g:
In Scheme:

(define print display)
(print "Hello world\n")
;; alternate way
(define print 'display)
((eval print) "Hello world\n")

The same approach does not seem to work in 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

I'm getting this error with Attempt 1 above:

*** - EVAL: undefined function P

And this with 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

And with gcl:

Error: (EVAL P) is invalid as a function.
Error: (EVAL (ENVIRONMENT) P) is invalid as a function.

So:

  • What does try using a symbol mean? p is definitely a symbol; false positive?
  • What's up with eval? Doesn't the evaluation of p yield the procedure print?
  • I thought Lisp procedures were first class objects. Why is Attempt 1 not working like in Scheme?

EDIT
(Moved from a comment below)

I was wondering why (setf (symbol-function 'p) #'print) won't work this way
(setf (symbol-function 'p) 'print). I get the following(not so helpful) error:

*** - SYSTEM::%PUTD: PRINT is not a function ;; CLisp
Error: PRINT is not of type LIST. ;; Gcl

I know that the sharp sign(#) is supposed to disambiguate between a function and a variable
with the same name but in this case, there's only one print, the function.

Also, why won't it work with defvar instead of setf like so:

(defvar (symbol-function 'p) #'print)

yet defvar and setf both assign values to a variable.
The associated error is:

*** - DEFVAR: non-symbol (SYMBOL-FUNCTION 'P) cannot be a variable ;; Clisp
Error: (SYMBOL-FUNCTION (QUOTE P)) is not of type SYMBOL. ;; Gcl

解决方案

Common Lisp is a "Lisp-2". Among other things, the first position in a function call is evaluated in the "function namespace". In your case, the symbol p names a variable, not a function.

This works better:

(defvar p 'print)

(funcall p "Hello world")

Or possibly, but you probably don't want to do this:

(setf (symbol-function 'p) #'print)

(p "Hello world")

这篇关于如何在Lisp中的变量中存储函数并使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆