LISP Exercice Fam Tr -


family tree exercise

i'm trying exercise this:

(defconstant *family-tree*    '((father barack pat) (mother michelle pat)     (father georgew peter) (mother laura peter)     (father geogerh james (mother barbara james)      (father bill jane) (mother hillary jane)      (father james mark) (mother jane mark)      (father peter mary) (mother pat mary)      (father mark john) (mother mary john)))) 

but i'm not sure if best way. moreover, have no idea how create function "parents" , "grandparents". appreciate help. thanks

you might want start baby steps:

(defun make-person (name &optional father mother)   (cons name (cons father mother))) 

now can create family tree:

(defconstant *family-tree*    (make-person 'john      (make-person 'mark         (make-person 'james          ...)        (make-person 'jane          ...))      (make-person 'mary        (make-person ...)        (make-person ...)))) 

finding person in tree requires recursion:

(defun find-person (tree name)   (if (eq (car tree) name)       tree       (or (find-person (cadr tree) name)           (find-person (cddr tree) name)))) 

where can replace cadr father-tree , cddr mother-tree. (you can define tree-name call car).

finding parents , grandparents should easy:

(defun parents (tree name)    (let ((person (find-person tree name)))      (list (caadr person) ; or (tree-name (tree-father person))            (caddr person)))) ; or ... 

Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -