Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (defconstant uncolored 'uncolored)
- (defclass node ()
- ((name :accessor name :initarg :name)
- (color :accessor color :initarg :color :initform uncolored)))
- (defclass graph ()
- ((nodes :accessor V :initform (make-hash-table)) ; name -> node
- (edges :accessor E :initform (make-hash-table)) ; name -> (name)
- ))
- (defun add-node (graph name)
- (let ((new-node (make-instance 'node :name name)))
- (setf (gethash name (v graph)) new-node)
- 'ok
- ))
- (defun get-node (graph name)
- (gethash name (v g)))
- (defun get-connections (graph name)
- (gethash name (e g)))
- ; This works
- (defun connect-nodes (graph name1 name2)
- (let ((node1 (get-node g name1)) (node2 (get-node g name2)))
- (unless (or (eq node1 'nil) (eq node2 'nil))
- (setf (gethash name1 (e g)) (adjoin name2 (get-connections g name1)))
- (setf (gethash name2 (e g)) (adjoin name1 (get-connections g name2)))
- 'ok
- )))
- ; This fails
- (defun connect-nodes-2 (graph name1 name2)
- (let ((node1 (get-node g name1)) (node2 (get-node g name2)))
- (unless (or (eq node1 'nil) (eq node2 'nil))
- (setf (get-connections g name1) (adjoin name2 (get-connections g name1)))
- (setf (get-connections g name2) (adjoin name1 (get-connections g name2)))
- 'ok ; ^ Aparently this isn't defined?
- )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement