読者です 読者をやめる 読者になる 読者になる

by shigemk2

当面は技術的なことしか書かない

対話によるCommon Lisp入門 43 二重回帰2 sublis

読書ノート
(sublis alist tree)

は、treeの中のアトムを、alistに従って書き換えるお。
こんな風に。

[4]> (sublis '((one . 1) (two . 2) (three . 3))
'((one plus one) equal two))
((1 PLUS 1) EQUAL 2)
[5]> (defun sublis$ (alist tree)
(if (atom tree)
(if (assoc tree alist)
(rest (assoc tree alist))
tree)
(cons (sublis$ alist (first tree))
(sublis$ alist (rest tree)))))
SUBLIS$
[6]> (sublis$ '((one . 1) (two . 2) (three . 3))
'((one plus one) equal two))
((1 PLUS 1) EQUAL 2)

トレース。

1. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) '((ONE PLUS ONE) EQUAL TWO))
2. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) '(ONE PLUS ONE))
3. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'ONE)
3. Trace: SUBLIS$ ==> 1
3. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) '(PLUS ONE))
4. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'PLUS)
4. Trace: SUBLIS$ ==> PLUS
4. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) '(ONE))
5. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'ONE)
5. Trace: SUBLIS$ ==> 1
5. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'NIL)
5. Trace: SUBLIS$ ==> NIL
4. Trace: SUBLIS$ ==> (1)
3. Trace: SUBLIS$ ==> (PLUS 1)
2. Trace: SUBLIS$ ==> (1 PLUS 1)
2. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) '(EQUAL TWO))
3. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'EQUAL)
3. Trace: SUBLIS$ ==> EQUAL
3. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) '(TWO))
4. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'TWO)
4. Trace: SUBLIS$ ==> 2
4. Trace: (SUBLIS$ '((ONE . 1) (TWO . 2) (THREE . 3)) 'NIL)
4. Trace: SUBLIS$ ==> NIL
3. Trace: SUBLIS$ ==> (2)
2. Trace: SUBLIS$ ==> (EQUAL 2)
1. Trace: SUBLIS$ ==> ((1 PLUS 1) EQUAL 2)
((1 PLUS 1) EQUAL 2)