Advertisement
Guest User

Untitled

a guest
May 7th, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.31 KB | None | 0 0
  1. (defconstant uncolored 'uncolored)
  2.  
  3. (defclass node ()
  4.   ((name :accessor name :initarg :name)
  5.    (color :accessor color :initarg :color :initform uncolored)))
  6.  
  7. (defclass graph ()
  8.   ((nodes :accessor V :initform (make-hash-table)) ; name ->  node
  9.    (edges :accessor E :initform (make-hash-table)) ; name -> (name)
  10.    ))
  11.  
  12. (defun add-node (graph name)
  13.   (let ((new-node (make-instance 'node :name name)))
  14.     (setf (gethash name (v graph)) new-node)
  15.     'ok
  16.     ))
  17.  
  18. (defun get-node (graph name)
  19.   (gethash name (v g)))
  20.  
  21. (defun get-connections (graph name)
  22.   (gethash name (e g)))
  23.  
  24. ; This works
  25. (defun connect-nodes (graph name1 name2)
  26.   (let ((node1 (get-node g name1)) (node2 (get-node g name2)))
  27.     (unless (or (eq node1 'nil) (eq node2 'nil))
  28.       (setf (gethash name1 (e g)) (adjoin name2 (get-connections g name1)))
  29.       (setf (gethash name2 (e g)) (adjoin name1 (get-connections g name2)))
  30.       'ok
  31.       )))
  32.  
  33. ; This fails
  34. (defun connect-nodes-2 (graph name1 name2)
  35.   (let ((node1 (get-node g name1)) (node2 (get-node g name2)))
  36.     (unless (or (eq node1 'nil) (eq node2 'nil))
  37.       (setf (get-connections g name1) (adjoin name2 (get-connections g name1)))
  38.       (setf (get-connections g name2) (adjoin name1 (get-connections g name2)))
  39.       'ok   ; ^ Aparently this isn't defined?
  40.       )))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement