Reputation: 1
So I recently taught myself lisp and have been messing around with some programs. I am trying to write a small program that compares a list of user-entered classes and finds which ones can work together. Below is the first part that collects the data from the user and creates some lists of when the class is.
(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)
)
When I evaluate the function it only returns 0 and nothing else appears. I'm not sure if it's because I am not using Listener or what. Thanks.
Upvotes: 0
Views: 593
Reputation: 85913
I don't think that the question is specified enough, but this may help you a bit, and hopefully serves as a useful example for decomposition and prompting. It's not too hard to add some additional checks to things:
(defun prompt (&optional (control "> ") &rest arguments)
"Read a value from STREAM after presenting PROMPT."
(fresh-line *terminal-io*)
(apply 'format *terminal-io* control arguments)
(finish-output *terminal-io*)
(read *terminal-io*))
(defstruct course
(name nil :type symbol :read-only t)
(day nil :type symbol :read-only t)
(start nil :type number :read-only t)
(end nil :type number :read-only t))
(defun prompt-for-course ()
(flet ((except-quit (x)
(if (string-equal (princ-to-string x) "quit")
(return-from prompt-for-course nil)
x)))
(let (name day start end)
(setq name (except-quit (prompt "course name: "))
day (except-quit (prompt "course day: "))
start (except-quit (prompt "start time: "))
end (except-quit (prompt "end time: ")))
(make-course :name name
:day day
:start start
:end end))))
(defun prompt-for-courses ()
(loop
with courses = (make-hash-table)
for course = (prompt-for-course)
while course
do (push course (gethash (course-day course) courses))
finally (return courses)))
CL-USER> (prompt-for-courses)
course name: intro-logic
course day: monday
start time: 1100
end time: 1250
course name: intermediate-logic
course day: tuesday
start time: 1100
end time: 1250
course name: quit
#<HASH-TABLE :TEST EQL :COUNT 2 {1003EA6513}>
Upvotes: 0