【发布时间】:2015-02-28 14:14:55
【问题描述】:
所以我最近自学了 lisp,并且一直在搞乱一些程序。我正在尝试编写一个小程序来比较用户输入的类列表并找出哪些可以一起工作。下面是第一部分,它从用户那里收集数据并创建一些班级时间的列表。
(defun class-entries ()
(setf Monday 0) ;initializes the days of the week
(setf Tuesday 0)
(setf Wednesday 0)
(setf Thursday 0)
(setf Friday 0)
(setf times 100)
(dotimes (repeating times "You have reached the limit of entries") ;repeats the following for the number of classes that you are comparing
(dolist (dayofweek '(Monday Tuesday Wednesday Thursday Friday)) ;repeats the following for each day of the week
(print (concatenate 'string "Does this class occur on " dayofweek " ?"))
(setf isday (read))
(if (= isday 1) ;prompts the questions if there is a class that day
(progn
(print (concatenate 'string "What time does the class begin on " dayofweek " ?"))
(setf starttime (read))
(print (concatenate 'string "What time does the class end on " dayofweek " ?"))
(setf endtime (read)))
(setf isday 0))
(if (= isday 0) ;Adds the list of (startime endtime) to the current day of week or nil if there isn't a class that day
(setf 'dayofweek '"nil")
(setf 'dayofweek '(starttime endtime))))
(print "What is the title of the class?")
(setf (read) '((mon (Monday)) (tues (Tuesday)) (wed (Wednesday)) (thurs (Thursday)) (fri (friday)))) ;sets a new variable to the values of the classes's hours
(print "Is there another class?") ;repeats the function for another class or ends it
(setf isclass (read))
(if (= isclass 0)
(setf times 0)
()))
(setf times 100)
)
当我评估该函数时,它只返回 0 并且没有其他内容出现。我不确定是不是因为我没有使用 Listener 还是什么。谢谢。
【问题讨论】:
-
(setf (read) ...)没有意义。(read)不是您可以分配的地方。这真的是编译没有错误吗? -
你也不应该在函数中使用全局变量。您应该使用
let绑定局部变量,而不是使用setf分配全局变量。 -
分配变量时,不应引用该变量。
(setf 'dayofweek '(starttime endtime))应该是(setf dayofweek (list starttime endtime)) -
(setf (read) 是为了分配一个用户立即为其提供名称的变量。是的,没有错误。但可能会出现问题,因为用户输入不起作用。我会尝试更多地使用局部变量,谢谢。我引用了该变量,因为我认为这会使其将该变量的值作为名称插入。由于该行在函数(dolist)中,因此 dayofweek 的值应该是其中之一天,希望正在处理的日期将其值设置为 '(starttime endtime)。
-
对于像 (setf isday (read)) 之类的东西,请注意 Common Lisp 已经提供了返回布尔值的 yes-or-no-p and y-or-n-p。
标签: input lisp common-lisp lispworks