如何列出LISP中1800年的所有闰年?

问题描述 投票:0回答:1

我在下面有这个代码,它接受一个参数并以相反的顺序打印所有闰年列表。如何才能将1800作为默认输入并运行命令(leap)列出1800-2018的所有闰年?

码:

(defun leap (q)

    (if (< q 1800)
        (RETURN-FROM leap nil)
    )

    (leap (- q 1))

    (if (leapyear q)
        (push q mylist)
    )
    mylist
)


(reverse(leap 2018))
lisp common-lisp clisp
1个回答
2
投票

我不能完全理解你想要做什么,但是:

(defun leapyearp (y)
  ;; is Y a leap year, as best we can tell?
  (= (nth-value 3 (decode-universal-time
                   (+ (encode-universal-time 0 0 0 28 2 y)
                      (* 60 60 24))))
     29))

(defun leapyears (&key (start 1800) (end (nth-value 5 (get-decoded-time))))
  ;; all the leap years in a range
  (loop for y from start to end
        if (leapyearp y) collect y))
© www.soinside.com 2019 - 2024. All rights reserved.