Lisp练习-打印月历

;;;; print 12 months accroding index in a week

;; if year is leap year?
(defun is-leap-year (year)
  (or 
   (and 
    (= 0 (mod year 4))
    (/= 0 (mod year 100)))
   (= 0 (mod year 400))))

;; return n
;; n  means first day of the year is in the n-th day in the week.
(defun first-day-of-year (year)
  (let* ((x (- year 1)) (y (* x 365)))
    (loop for z from 1 to x
	 when (is-leap-year z)
	 do (incf y))
    (mod y 7)))

;; days-in-month
(defun days-in-month (year month)
  (if 
   (or
    (< month 1)
    (> month 12))
   (return-from days-in-month 0))
  (cond
    ((find month '(1 3 5 7 8 10 12)) (return-from days-in-month 31))
    ((find month '(4 6 9 11)) (return-from days-in-month 30))
    ((is-leap-year year) (return-from days-in-month 29))
    (28)))

;; print month table.
(defun print-month-table (year)
  (let ((week-index (first-day-of-year year)))
    (format t "week-index: ~d~%" week-index)
    (loop for month from 1 to 12
       do
	 (format t "~d-~d~%" year month)
	 (format t "SUN  MON  TUE  WEN  THR  FRI  SAT~%")
	 (format t "=================================~%")
	 (loop repeat (mod (+ week-index 1) 7)
	    do
	      (format t "     "))
	 (loop for day from 1 to (days-in-month year month)
	    do
	      (format t "~2d   " day)
	      (setf week-index (mod (+ week-index 1) 7))
	      (if (= 6 week-index)
		  (format t "~%")))
	 (format t "~%")))) 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值