Common Lisp 中的河内塔
我认为这将是一个模糊的问题,因为我一开始并不确切地知道我在做什么,但事实就是如此。
我必须使用列表在 Common Lisp 中解决河内塔问题。基本上,一个函数获取一个字符串(名称)列表,然后使用 peg B 将它们从 peg A 移动到 peg C 进行存储,并保持它们在列表中的顺序相同。
我以前从未使用过 lisp,我发现它的语法很难理解。 这是我到目前为止的代码 goo 函数是河内工作
(defparameter A '())
(defparameter B '())
(defparameter C '())
(defun findPeg (p1 p2) (cond ((= 0 (- 3 p1 p2))A)
((= 1 (- 3 p1 p2))B) ((= 2 (- 3 p1 p2))C)))
(defun getnum (x) (cond ((equalp x A) 0)((equalp x B)1)((equalp x C) 2)))
(defun hanoi (x) (defparameter A x) (setlength A)(goo len A C B))
(defun setlength(x) (defparameter len (list-length x)))
(defun goo (leng from to via)
(cond ((= leng 1)(push (pop A) C)) ;base case
((goo (1- leng) from via to)(push (pop A) B) ;say/do something i think
((goo (1- leng) via to from)(push (pop B) C) ;say/do something i think
))))
我的问题是递归调用。我很困惑我到底应该做什么。我知道我显然必须将第一个列表中的第一个字符串移动到另一个挂钩,但我不知道哪个挂钩,甚至不知道如何操作列表。我觉得我应该使用传递到 goo 函数的变量,但我不知道如何编辑它们,因为当我在函数中更改它们时,外面的变量不会改变。
现在我遇到了错误
* - SYSTEM::%EXPAND-FORM: (GOO (1- LENG) FROM VIA TO) should be a lambda expression
This is a recursive call so I don'不知道为什么这么说。
基本上我只是想要一些关于在哪里继续或在哪里重新启动的提示或技巧,因为我什至不知道我的方法是否好。 任何事情都非常感激。谢谢
I think this is going to be a vague question because I don't know exactly what I am doing in the first place but here it goes.
I have to do a towers of hanoi problem in common lisp using lists. Basically a function takes a list of strings (names) and then moves them from peg A to peg C using peg B for storage, keeping them in the same order as they were in the list.
I have never used lisp before and I find the syntax very hard to understand.
This is my code so far
goo function is the hanoi work
(defparameter A '())
(defparameter B '())
(defparameter C '())
(defun findPeg (p1 p2) (cond ((= 0 (- 3 p1 p2))A)
((= 1 (- 3 p1 p2))B) ((= 2 (- 3 p1 p2))C)))
(defun getnum (x) (cond ((equalp x A) 0)((equalp x B)1)((equalp x C) 2)))
(defun hanoi (x) (defparameter A x) (setlength A)(goo len A C B))
(defun setlength(x) (defparameter len (list-length x)))
(defun goo (leng from to via)
(cond ((= leng 1)(push (pop A) C)) ;base case
((goo (1- leng) from via to)(push (pop A) B) ;say/do something i think
((goo (1- leng) via to from)(push (pop B) C) ;say/do something i think
))))
My problem is with the recursive calls. I am very confused as to what exactly I should be doing. I know I obviously have to move the first string in the first list to another peg, but I don't know which peg or even how to manipulate the lists. I feel like I should be using the variables that were passed into the goo function, but I cant figure out how to edit them because when I change them in the function the variables outside do not change.
Right now I am running into the error
* - SYSTEM::%EXPAND-FORM: (GOO (1- LENG) FROM VIA TO) should be a lambda expression
This is a recursive call so I don't know why it is saying that.
Basically I just want some tips or tricks on where do continue or ever where to restart because I don't even know if my approach is a good one.
Anything is greatly appreciated. Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,在 DEFUN 中使用 defparameter 几乎从来都不是正确的做法。
如果您想要一个词法范围的变量,请使用 LET (或者简单地按照您希望的方式命名您的形式参数)。
其次,您的 GOO 函数内有
((fun arg ..) (fun arg ...))
形式的内容。你想失去最外面的括号。First off, using defparameter inside a DEFUN is almost never the right thing to do.
If you want to have a lexically-scoped variable, use LET (or simply name your formal parameters as you'd like them named).
Second, you have something on the form
((fun arg ..) (fun arg ...))
inside your GOO function. You want to lose the outermost parentheses.