开发者

towers of hanoi in common lisp

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜