函数参数如何存储在lisp中? [英] How are function parameters stored in lisp?
问题描述
我假定传递给lisp函数的值被分配给一个匹配参数名称的引用。然而,我感到惊讶的是:
(defun test(x)(print(eval'x)))
(测试5)
不起作用(变量x未绑定)。所以如果参数没有作为符号存储在函数中,那么在这个例子中究竟是什么?有没有办法从符号参数名称的符号访问参数?
更多的上下文:
我想要做的是这样的:
defun slice(r1 c1 r2 c2 board)
(dolist(param'(r1 c1 r2 c2)); adjust for负数指数
(如果(<(eval param)0)
(set param(+(length board)(eval param)))))
;函数体
基本上,我想遍历前四个参数并对它们的任何值进行调整,如果它们是< 0.当然,我可以为每个参数做一个独立的行,但考虑到我对四个参数中的每一个都做了同样的事情,这看起来更清晰。
但是,我得到的错误是变量R1未被绑定。
有没有一种方法可以从匹配参数名称的符号中访问参数?
不适用于词法绑定。 Common Lisp无法从类似的命名符号访问词法变量。您需要声明变量特殊。
因此,如果参数不作为符号存储在函数中,在这个例子中究竟是什么x?
处理器寄存器?一个堆栈框架的一部分?
使用动态绑定:
CL-USER 40> (defun foo(ab)
(declare(special ab))
(dolist(v'(ab))
(if(zerop(symbol-value v))
设置v 10)))
(值ab))
FOO
CL-USER 41> (foo 1 0)
1
10
I assumed that values passed into a lisp function are assigned to a quote matching the name of the parameter. However, I was surprised that this:
(defun test (x) (print (eval 'x)))
(test 5)
doesn't work (the variable x is unbound). So if parameters aren't stored as symbols in the function, what exactly IS x in this example? Is there a way to access parameters from a symbol matching the parameter name?
More context: What I would like to do is something like this:
defun slice (r1 c1 r2 c2 board)
(dolist (param '(r1 c1 r2 c2)) ;adjust for negative indices
(if (< (eval param) 0)
(set param (+ (length board) (eval param)))))
;Body of function
Basically, I want to iterate through the first four parameters and make an adjustment to any of their values if they are < 0. Of course, I could do a let and have an individual line for each parameter, but considering I'm doing the same thing for each of the four parameters this seemed cleaner. However, I get the error that the variable R1 is unbound.
Is there a way to access parameters from a symbol matching the parameter name?
Not for lexical binding. Common Lisp gives no way to access a lexical variable from a similar named symbol. You would need to declare the variable special.
So if parameters aren't stored as symbols in the function, what exactly IS x in this example?
A processor register? A part of a stack frame?
With dynamic binding:
CL-USER 40 > (defun foo (a b)
(declare (special a b))
(dolist (v '(a b))
(if (zerop (symbol-value v))
(set v 10)))
(values a b))
FOO
CL-USER 41 > (foo 1 0)
1
10
这篇关于函数参数如何存储在lisp中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!