世界のナベアツ

Rubyist SNS世界のナベアツが話題になっていたので、emacs lisp で作ってたみた。

(defun nabep (n &optional base)
  "n は あほ になる数か?"
  (or base (setq base 3))
  (cond
   ((= (mod n base) 0)  t)
   ((string-match (format "%d" base) (format "%d" n))  t)
   (t  nil)))

(defun count-of-nabe (n)
  "ナベアツに n を読んでもらう"
  (cond
   ((and (nabep n)
	 (= (mod n 5) 0)) (format "%d あほ犬" n))
   ((= (mod n 5) 0)       (format "%d 犬" n))
   ((nabep n)             (format "%d あほ" n))
   (t                     (format "%d" n))))

(defun nabe-of-the-world (n)
  "ナベアツが 1 〜 n まで読んだ結果をリストで返す"
  (let (nabe)
    (while (> n 0)
      (setq nabe (cons (count-of-nabe n) nabe))
      (setq n (- n 1)))
    nabe))

普通の FizzBuzz と違って、3 の処理がスッキリ行かない。

動かしてみる。

(let ((omoro (nabe-of-the-world 40)))
  (while omoro
    (message (car omoro))
    (sit-for 1)
    (setq omoro (cdr omoro))))

メッセージバッファの内容。

1
2
3 あほ
4
5 犬
6 あほ
7
8
9 あほ
10 犬
11
12 あほ
13 あほ
14
15 あほ犬
16
17
18 あほ
19
20 犬
21 あほ
22
23 あほ
24 あほ
25 犬
26
27 あほ
28
29
30 あほ犬
31 あほ
32 あほ
33 あほ
34 あほ
35 あほ犬
36 あほ
37 あほ
38 あほ
39 あほ
40 犬