とりあえず構造体とハッシュをつかって。treeのユーティリティは
ANSI Common Lispにとりあげられていたよ。
DOKAKU178> (maphash #'(lambda(k v)
(print-tree k)) *population*)
NIL => A => (F B)
(A) => B => (C)
(Z B) => C => (E D)
(C) => D => (X)
(C) => E => NIL
(A) => F => NIL
(D) => X => NIL
NIL => Y => (Z)
(Y) => Z => (C)
ytakenaka
#6344()
[
Common Lisp
]
Rating0/0=0.00
(defpackage :dokaku178 (:use :common-lisp)) (in-package :dokaku178) (defvar *population* (make-hash-table)) (defstruct tree id children parents) (defun get-node (key) (multiple-value-bind (hash-value flag) (gethash key *population*) (if (null flag) (setf (gethash key *population*) (make-tree :id key :parents nil :children nil)) hash-value))) (defun set-node (pair-list) (let* ((parent (car pair-list)) (child (cdr pair-list)) (parent-node (get-node parent)) (child-node (get-node child))) (pushnew parent (tree-parents child-node)) (pushnew child (tree-children parent-node)))) (defun get-origins () (let ((origins nil)) (maphash #'(lambda(k v) (if (null (tree-parents v)) (pushnew k origins))) *population*) origins)) (defun print-tree (id) (let ((id (gethash id *population*))) (format t "~a => ~a => ~a ~%" (tree-parents id) (tree-id id) (tree-children id)))) (defun main() (let ((pair-lst '((a . b)(b . c)(c . d)(c . e) (a . f)(d . x)(y . z)(z . c)))) (loop for n in pair-lst do (set-node n))))Rating0/0=0.00-0+
[ reply ]