关于执行命令的问题 [英] Questions about Execution Order
问题描述
我正在尝试学习Common Lisp,并且在repl中尝试某些操作时发现了一些意外(对我而言).基于大多数编程语言中的执行顺序,以及我一直从lisp听到的一流的一流功能支持,我认为以下方法应该有效:
I'm trying to learn Common Lisp, and found something unexpected (to me) when trying something out in the repl. Based on order of execution in most programming languages, and the great first class function support I'd always heard about from lisp, I'd think the following should work:
((if t 'format) t "test")
在Ruby中,我可以这样做:
In Ruby I can do:
if true
Object.method(:puts)
end.call("test")
我对上面的lisp代码如何工作的思考是,它应该评估内部lisp形式,返回格式,然后开始评估外部lisp形式,并以format
作为第一个原子.我已经读到第一种形式必须是符号,所以即使我最初的想法是在从内部形式返回之前尝试评估格式,我也尝试了((if t format) t "test")
.
My thinking in how the above lisp code should work is that it should evaluate the inner lisp form, return format, then begin evaluating the outer lisp form, with format
then being the first atom. I'd read that the first form needs to be a symbol, so I also tried ((if t format) t "test")
even though my initial thought was that this would try to evaluate format before returning from the inner form.
我已经注意到,有时lisp形式需要在#'
之前,以便它们的结果可被调用,但是使用(#'(if t 'format) t "test")
也不起作用.我确定我只是误解了一些基本知识,因为我对Lisp相当陌生,但是这里发生了什么?
I've noticed that sometimes lisp forms need to be preceded by #'
in order for their results to be callable, but using (#'(if t 'format) t "test")
doesn't work either. I'm sure I'm just misunderstanding something basic as I'm pretty new to lisp, but what's going on here?
推荐答案
Common Lisp通常不对表达式的第一个元素求值.它必须是为函数命名的文字符号或lambda表达式.
Common Lisp doesn't evaluate the first element of an expression normally. It has to be either a literal symbol naming a function, or a lambda expression.
If you want to call a function determined dynamically, you need to use the FUNCALL
function:
(funcall (if t 'format) t "test")
这类似于在Ruby中使用.call()
方法的需要.
This is analogous to the need to use the .call()
method in Ruby.
您尝试过的方法可以在其他Lisp方言中使用,例如Scheme.
What you tried would work in some other Lisp dialects, such as Scheme.
这篇关于关于执行命令的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!