(defun mappend (fn the-list) (apply #'append (mapcar fn the-list)))
(defun generate (phrase) "Generate a random sentence or phrase" (let ((possible-rewrites (rewrites phrase))) (cond ((listp phrase) (mappend #'generate phrase)) (possible-rewrites (generate (random-elt possible-rewrites))) (t (list phrase)))))
規則のリストが既定のとおりであるならば、解答例のように non-terminal-p の処理を (not (null (...))) と書かなくてもよいだろう。
(defun generate (phrase) "Generate a random sentence or phrase" (cond ((listp phrase) (mappend #'generate phrase)) ((non-terminal-p phrase) (generate (random-elt (rewrites phrase)))) (t (list phrase)))) (defun non-terminal-p (category) (rewrites category))
Xバー理論を利用すればよいだろう。
(defun cross-product (fn xlist ylist) (mappend #'(lambda (y) (mapcar #'(lambda (x) (funcall fn x y)) xlist)) ylist)) (defun combine-all (xlist ylist) (cross-product #'append xlist ylist)) (combine-all '((a) (b)) '((1) (2))) ; => ((A 1) (B 1) (A 2) (B 2)) (cross-product #'+ '(1 2 3) '(10 20 30)) ; => (11 12 13 21 22 23 31 32 33) (cross-product #'list '(a b c d e f g h) '(1 2 3 4 5 6 7 8)) ; => ((A 1) (B 1) (C 1) (D 1) (E 1) (F 1) (G 1) (H 1) (A 2) (B 2) (C 2) (D 2) (E 2) ; (F 2) (G 2) (H 2) (A 3) (B 3) (C 3) (D 3) (E 3) (F 3) (G 3) (H 3) (A 4) (B 4) ; (C 4) (D 4) (E 4) (F 4) (G 4) (H 4) (A 5) (B 5) (C 5) (D 5) (E 5) (F 5) (G 5) ; (H 5) (A 6) (B 6) (C 6) (D 6) (E 6) (F 6) (G 6) (H 6) (A 7) (B 7) (C 7) (D 7) ; (E 7) (F 7) (G 7) (H 7) (A 8) (B 8) (C 8) (D 8) (E 8) (F 8) (G 8) (H 8))
(defun print-cross-product (fn xlist ylist) (mapc #'(lambda (y) (print (mapcar #'(lambda (x) (funcall fn x y)) xlist))) ylist)) (print-cross-product #'+ '(1 2 3) '(10 20 30)) ; => (print-cross-product #'list '(a b c d e f g h) '(1 2 3 4 5 6 7 8)) ; =>