mapl

ちょっと前に覚えたmaplist系の関数を使いたくてCommonLispスレの17を書いてみた。

(defun numlist (nums c s)
  (if (= c 1)
      (if (member s nums) `((,s)) nil)
      (let (result)
        (mapl #'(lambda (l)
                  (aif (numlist (cdr l) (1- c) (- s (car l)))
                       (mapc #'(lambda (r) (push (cons (car l) r) result)) it)
                       nil)) nums)
      result)))

実行

 * (numlist '(1 2 3 4 5 6 7 8 9 10) 3 20)

((5 6 9) (5 7 8) (4 6 10) (4 7 9) (3 7 10) (3 8 9) (2 8 10) (1 9 10))

aifはOnLispの14章から。(ソース)

maplistとかmaplってどういう時に使うんだろ、って最初思ったけど

(defun hoge (l)
  (do ((a (car l) (car b))
       (b (cdr l) (cdr b)))
      ((null a))
    ...))

みたいなコードをどっかで書いてたような気がしないでもない。
お決まりの題材とかどっかにないかな。