所以我知道通常这个错误意味着我在某处忘记了一个括号(或类似的东西),但对于我的生活,我无法弄清楚我哪里出错了。
我似乎得到的具体错误是:(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)
如果没有正确的格式和缩进,您将在编程中一事无成。
您的代码:
(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
??? 遍历列表
defvar
或 defparameter
来定义全局变量(并考虑使用"耳罩"来表示它们,所以(defvar *exotic-cars* ...)
)。
使用 let
声明和初始化局部变量(因此(let ((cnt 0)) ...)
)。尽管在这种情况下,您可能会最好使用 (loop for cnt from 0 to 9 ...)
,因为似乎不需要cnt
循环之外。
列表并不神奇,如果你想有一个代码块,请考虑使用 progn
.