練習問題/解答例/世界のナベアツ問題/Common Lisp
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
**Common Lisp [#a7e1f7f2]
;; ユーティリティ
(defun find-num (x num)
(unless (zerop num)
(if (= (mod num 10) x)
t
(find-num x (truncate num 10)))))
(defun divisible-p (num div)
(zerop (mod num div)))
(defun say (x)
(format t "~a~%" x))
(defun make-cycle (lst)
(let ((backup-list (copy-list lst)))
#'(lambda ()
(or (pop lst)
(and (setq lst backup-list) nil)))))
;; ここからナベアツ部分
(defun aho-p (n)
(or (divisible-p n 3)
(find-num 3 n)))
(defun make-nabeatsu (max)
(loop for n from 1 to max
collect (if (aho-p n) 'aho n)))
(defun make-nabeatsu-closure (max)
(make-cycle (make-nabeatsu max)))
(defun nabeatsu (max)
(let ((c (make-nabeatsu-closure max)))
(loop (let ((x (funcall c)))
(if x (say x) (return))))))
終了行:
**Common Lisp [#a7e1f7f2]
;; ユーティリティ
(defun find-num (x num)
(unless (zerop num)
(if (= (mod num 10) x)
t
(find-num x (truncate num 10)))))
(defun divisible-p (num div)
(zerop (mod num div)))
(defun say (x)
(format t "~a~%" x))
(defun make-cycle (lst)
(let ((backup-list (copy-list lst)))
#'(lambda ()
(or (pop lst)
(and (setq lst backup-list) nil)))))
;; ここからナベアツ部分
(defun aho-p (n)
(or (divisible-p n 3)
(find-num 3 n)))
(defun make-nabeatsu (max)
(loop for n from 1 to max
collect (if (aho-p n) 'aho n)))
(defun make-nabeatsu-closure (max)
(make-cycle (make-nabeatsu max)))
(defun nabeatsu (max)
(let ((c (make-nabeatsu-closure max)))
(loop (let ((x (funcall c)))
(if x (say x) (return))))))
ページ名: