Write a lisp function that prints out the calendar for the current month. This function should take 2 parameters: the day of the week that the month starts from as an index (0 for Sunday, etc.) and the number of days in the month. For example, if you call the function (calendar 2 30) Print the initial of each day on the first line using the list of days defined at the previous exercise, the capitalized symbol-name as before, and the function substring to extract the first letter of the string. For example, the call: (substring "Sunday" 0 1) returns the string "S".
Write a lisp function that prints out the calendar for the current month. This function should take 2 parameters: the day of the week that the month starts from as an index (0 for Sunday, etc.) and the number of days in the month. For example, if you call the function
(calendar 2 30)
Print the initial of each day on the first line using the list of days defined at the previous exercise, the capitalized symbol-name as before, and the function substring to extract the first letter of the string. For example, the call:
(substring "Sunday" 0 1) returns the string "S".
Here's a possible solution in Common Lisp:
(defun calendar (start-day num-days)
(let ((days '("Sunday" "Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday"))
(cur-day start-day)
(cur-date 1))
;; print header row
(dolist (day days)
(format t "~3a " (substring day 0 1)))
(terpri)
;; print calendar
(dotimes (i start-day)
(format t " "))
(dotimes (i num-days)
(format t "~3d " cur-date)
(when (= cur-day 6)
(terpri))
(incf cur-day)
(when (= cur-day 7)
(setq cur-day 0))
(incf cur-date))
(terpri)))
Step by step
Solved in 2 steps