■
[xyzzy] ツリーを出力するlisp
リストからツリーを出力するlispを書いた。
以下のように書くと、
(draw-tree "" '(AAA (AAA1 AAA2 AAA3) BBB CCC))
以下のような文字列を出力します。
├ AAA | ├ AAA1 | ├ AAA2 | └ AAA3 | ├ BBB └ CCC
コードは以下のとおり。
(defun draw-tree (pre l) (if l (progn ;先頭の処理 (if (atom (car l)) ; 先頭がatomの場合、ノードを出力 (format t "~A~A ~A\n" pre (if (atom (second l)) (if (second l) "├" "└") (if (third l) "├" "└") ) (if (symbolp (car l)) (symbol-name(car l)) (car l))) ; 先頭がlistの場合、リストの中を再帰的に処理 (progn (draw-tree (if (second l) (concat pre "| ") (concat pre " ")) (car l)) (format t "~A\n" (if (second l) (concat pre "| ") (concat pre " "))))) ;残りの処理 (draw-tree pre (cdr l)))))