Lisp 重新创建临时变量
我在 Lisp 方面遇到了一些麻烦。我试图做的是跟踪一个数字在 x 个列表中出现的次数。然而,一遍又一遍地运行这个函数,lisp 并没有重新创建变量,而是使用我上次调用该函数时的结束值。所以我想知道如何才能超越 let 的“约束”权力?
所以,我有一些像这样的列表
(((8 7) (3)) ((8 3) (2)) ((7 3) (6)) ((7 2) (8)) ((6 7) (4 1))
((6 6) (4 1)) ((6 2) (2)) ((5 6) (3)) ((5 3) (8 3)) ((4 6) (4))
((4 4) (6)) ((4 1) (7)) ((3 7) (5 3)) ((3 4) (1)) ((3 3) (3)) ((3 1) (9))
((2 7) (7)) ((2 5) (2)) ((2 2) (5 2)) ((1 7) (1)) ((1 6) (6 1))
((1 1) (2 1)) ((1 0) (3)) ((0 7) (8 1)) ((0 5) (6)) ((0 3) (9 6))
((0 1) (1)))
然后我调用一些像这样的函数,(在初始函数调用之后,在这里声明 var 似乎没有做任何事情)...我猜想来自 let 的某种绑定。
(defun counter (possibleValues)
(let ((var '(0 0 0 0 0 0 0 0 0 0)))
(loop for i from 0 to (list-length possibleValues) do
(loop for j in (cdr (nth i possibleValues)) do
(loop for k in j do
(incf (nth k var)))))
var))
因此,我可以通过该函数运行我的列表,并获得类似于
(0 8 5 6 3 2 5 2 3 2)
每个位置引用列表中找到的数字的内容。所以值 8 指的是在所有列表中找到 1 的次数(我只考虑第二个列表)。现在的问题...运行它两次并且...
(0 16 10 12 6 4 10 4 6 4)
我之前使用了关联列表,但在试图解决这个问题并使事情变得简单时,我现在使用一个列表。我想我的另一个问题是,如何动态创建关联列表元素?我不喜欢这样声明“var”,但我现在只是想绕过“let”。我对“setq”或“setf”也没有太多运气......
提前感谢您的帮助!
I'm having a bit of trouble with Lisp. What i'm attempting to do, is keep track of the amount of times a number appears in x number of lists. However, running this over and over again, lisp isn't recreating the variable, but using the ending value from the last time I called the function. So I'm wondering how can I get past the 'binding' powers of let?
So, I've got some list like this
(((8 7) (3)) ((8 3) (2)) ((7 3) (6)) ((7 2) (8)) ((6 7) (4 1))
((6 6) (4 1)) ((6 2) (2)) ((5 6) (3)) ((5 3) (8 3)) ((4 6) (4))
((4 4) (6)) ((4 1) (7)) ((3 7) (5 3)) ((3 4) (1)) ((3 3) (3)) ((3 1) (9))
((2 7) (7)) ((2 5) (2)) ((2 2) (5 2)) ((1 7) (1)) ((1 6) (6 1))
((1 1) (2 1)) ((1 0) (3)) ((0 7) (8 1)) ((0 5) (6)) ((0 3) (9 6))
((0 1) (1)))
Then I'm calling some function like this, (declaring var here doesn't seem to do anything, past the initial function call)... I guess some kind of binding from let.
(defun counter (possibleValues)
(let ((var '(0 0 0 0 0 0 0 0 0 0)))
(loop for i from 0 to (list-length possibleValues) do
(loop for j in (cdr (nth i possibleValues)) do
(loop for k in j do
(incf (nth k var)))))
var))
So I can run my list through the function and get something like
(0 8 5 6 3 2 5 2 3 2)
Each position referring to the number found in the list. So the value 8 would refer to how many times 1 was found in all the lists (i'm considering the second list only). Now the problem.... run it twice and...
(0 16 10 12 6 4 10 4 6 4)
I was using an associative list earlier, but in trying to figure this out and keep things simple, i'm now using a list. I guess another question I have is, how can I create associative list elements on the fly? I don't like declaring 'var' like that, but I'm just trying to get around 'let' for the moment. I haven't had much luck with 'setq' or 'setf' either....
Thanks in advance for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 VAR 的初始化形式更改为创建新列表的表达式,例如
(make-list 10 :initial-element 0)
甚至(list 0 0 0 0 0 0 0 0 0 0)
。基本上,如果您打算修改带引号的对象,请不要使用它们,因为如果这样做,后果是不确定的。事实上,评估该函数定义会发出警告:
Change the initialization form for VAR to be an expression that creates new lists, such as
(make-list 10 :initial-element 0)
or even(list 0 0 0 0 0 0 0 0 0 0)
.Basically, do not ever use quoted objects if you have intentions on modifying them, as the consequences are undefined if you do. In fact, evaluating that function definition gives a warning about that: