【问题标题】:(code) should be a lambda expression...what exactly am I missing?(代码)应该是一个 lambda 表达式......我到底错过了什么?
【发布时间】:2016-03-09 22:56:48
【问题描述】:

所以我知道,这个错误通常意味着我在某处忘记了括号(或类似的东西),但在我的一生中,我无法弄清楚我哪里出错了。

我似乎得到的具体错误是:(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)

【问题讨论】:

标签: lambda lisp common-lisp


【解决方案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)
    (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 不声明/定义变量。它只是设置声明/定义变量的值。

为什么要使用WHILENTH 遍历列表???

【讨论】:

    【解决方案2】:

    使用defvardefparameter 定义全局变量(并考虑使用“耳罩”向它们发出信号,所以(defvar *exotic-cars* ...))。

    使用let 来声明和初始化局部变量(所以(let ((cnt 0)) ...))。尽管在这种情况下,使用(loop for cnt from 0 to 9 ...) 可能会更好,因为您似乎不需要在循环之外使用cnt

    列表并不神奇,如果你想要一个代码块,考虑使用progn

    【讨论】:

      猜你喜欢
      • 2015-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-14
      • 2022-11-15
      • 2014-07-07
      • 1970-01-01
      相关资源
      最近更新 更多