所以我知道通常这个错误意味着我忘了某个地方的括号(或者那个效果),但对于我的生活,我无法弄清楚我哪里出错了。
我似乎得到的具体错误是:(SETF (GET (NTH CNT EXOTIC-CARS) 'MAKE) (READ MYSTREAM))
- 这基本上是我第一次定义函数的第一行。
整个代码在这里:
(setq exotic-cars '(car1 car2 car3 car4 car5 car6 car7 car8 car9 car10))
(defun dostuff ()
(fetchinput)
(printlist)
(findmake)
(mysort))
(defun fetchinput ()
(with-open-file (mystream "cars.dat")
(setq cnt 0)
(loop while (<= cnt 9)
do ((setf (get (nth cnt exotic-cars) 'make) (read mystream))
(setf (get (nth cnt exotic-cars) 'model) (read mystream))
(setf (get (nth cnt exotic-cars) 'cost) (read mystream))
(setf cnt (+ cnt 1))
)
)))
(defun findmake ()
(setf cnt 0)
(format t "~2%~A ~A ~A ~A ~A" 'Car 'make 'to 'search 'for>)
(setf search (read))
(loop while (< cnt 10)
do ((if (string= (get (nth cnt exotic-cars) 'make) search)
(format t "~2%~1,15T ~A ~2,15T ~A ~3,15T ~A ~% ~A~%~1,15T ~A ~2,15T ~A ~3,15T $~D.00" 'Make 'Model 'Cost
'-------------------------------------
(get (nth cnt exotic-cars) 'make)
(get (nth cnt exotic-cars) 'model)
(get (nth cnt exotic-cars) 'cost)))
(setf cnt (+ cnt 1)))))
(defun mysort ()
(setf unsorted 0)
(loop while (< unsorted 9)
do ((setf current 9)
(loop while (> current unsorted)
do ((if (<
(get (nth current exotic-cars) 'cost)
(get (nth (- current 1) exotic-cars) 'cost))
(swap current (- current 1)))
(setf current (- current 1))))
(setf unsorted (+ unsorted 1))))
(printlist))
(defun swap (from to)
(setf tmpmake (get (nth to exotic-cars) 'make))
(setf tmpmodel (get (nth to exotic-cars) 'model))
(setf tmpcost (get (nth to exotic-cars) 'cost))
(setf (get (nth to exotic-cars) 'make) (get (nth from exotic-cars) 'make))
(setf (get (nth to exotic-cars) 'model) (get (nth from exotic-cars) 'model))
(setf (get (nth to exotic-cars) 'cost) (get (nth from exotic-cars) 'cost))
(setf (get (nth from exotic-cars) 'make) tmpmake)
(setf (get (nth from exotic-cars) 'model) tmpmodel)
(setf (get (nth from exotic-cars) 'cost) tmpcost))
(defun printlist ()
(setf cnt 0)
(format t "~2%~1,15T ~A ~2,15T ~A ~3,15T ~A ~% ~A" 'Make 'Model 'Cost
'-------------------------------------)
(loop while (< cnt 10)
do ((format t "~%~1,15T ~A ~2,15T ~A ~3,15T $~D.00"
(get (nth cnt exotic-cars) 'make)
(get (nth cnt exotic-cars) 'model)
(get (nth cnt exotic-cars) 'cost))
(setf cnt (+ cnt 1)))))
(dostuff)
答案 0 :(得分:5)
如果没有正确的格式和缩进,您将无法进行编程。
您的代码:
(defun fetchinput ()
(with-open-file (mystream "cars.dat")
(setq cnt 0)
(loop while (<= cnt 9)
do ((setf (get (nth cnt exotic-cars) 'make) (read mystream))
(setf (get (nth cnt exotic-cars) 'model) (read mystream))
(setf (get (nth cnt exotic-cars) 'cost) (read mystream))
(setf cnt (+ cnt 1))
)
)))
正确格式化:
(defun fetchinput ()
(with-open-file (mystream "cars.dat")
(setq cnt 0)
(loop while (<= cnt 9)
do ((setf (get (nth cnt exotic-cars) 'make) (read mystream))
(setf (get (nth cnt exotic-cars) 'model) (read mystream))
(setf (get (nth cnt exotic-cars) 'cost) (read mystream))
(setf cnt (+ cnt 1))))))
代码有很多问题:
(defun fetchinput ()
(with-open-file (mystream "cars.dat")
(setq cnt 0) ; <- the variable CNT is undefined
(loop while (<= cnt 9)
do ( ; <- what is this parenthesis for ?
(setf (get (nth
cnt
exotic-cars) ;<- the variable exotic-cars is undefined
'make) (read mystream))
(setf (get (nth cnt exotic-cars) 'model) (read mystream))
(setf (get (nth cnt exotic-cars) 'cost) (read mystream))
(setf cnt (+ cnt 1))
) ; < - what is this parenthesis for?
)))
SETF
未声明/定义变量。它只是设置声明/定义变量的值。
为什么要使用WHILE
和NTH
???
答案 1 :(得分:1)
使用defvar
或defparameter
定义全局变量(并考虑使用“耳罩”来表示它们,因此(defvar *exotic-cars* ...)
)。
使用let
声明和初始化局部变量(所以(let ((cnt 0)) ...)
)。虽然在这种情况下,使用(loop for cnt from 0 to 9 ...)
可能会更好,因为它似乎不需要在循环外部cnt
。
列表并不神奇,如果您想要一段代码,请考虑使用progn
。