[FizzBuzz] Ver.1.1
;; FizzBuzz -*- coding; iso-2022-jp -*-
;; Ver. 1.1
(defun FizzBuzz ()
"Fizz-Buzz program. that from 1 to 100 processed."
(interactive)
(let ((min 1)
(max 100))
(message "Start FizzBuzz %d to %d" min max)
(FizzBuzz-control min max 'FizzBuzz-normal)))
(defun FizzBuzz-control (min max &optional func)
"Process FizzBuzz normal or optional func."
(let ((buf (get-buffer-create "*FizzBuzz*"))
(fizz 0)
(buzz 0)
(func (or func 'FizzBuzz-normal)))
(when (>= min max)
(throw 'error
"Value ileagal"))
(buffer-disable-undo buf)
(pop-to-buffer buf)
(with-current-buffer buf
(toggle-read-only -1)
(erase-buffer)
(funcall func min max)
(set-buffer-modified-p nil)
(toggle-read-only 1)
(message "%s:%d %s:%d" "Fizz" fizz "Buzz" buzz))))
(defun FizzBuzz-normal (min max)
"Normal while loop FizzBuzz"
(let ((num min)
mod3 mod5)
(while (<= num max)
(setq mod3 (% num 3)
mod5 (% num 5))
(progn
(cond
((and (eq mod3 0) (not (eq mod5 0)))
(insert "Fizz")
(incf fizz))
((and (not (eq mod3 0)) (eq mod5 0))
(insert "Buzz")
(incf buzz))
((and (eq mod3 0) (eq mod5 0))
(insert "FizzBuzz")
(incf fizz)
(incf buzz))
(t
(insert (format "%d" num))))
(insert "\n")
(incf num)))))
呼び出し口の関数とMVCでのV+C相当?のcontrolおよび具象関数としてnormalに分離
fizzとbuzzがそのまま利用されたりしているのが微妙
次はnormalに入れ替えできる再帰関数+剰余演算子なし版を考えてみるか?
[FizzBuzz] Ver.1.1 More ログイン