Advertisement
Guest User

Untitled

a guest
Mar 18th, 2022
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 1.99 KB | None | 0 0
  1.  
  2. (defun lh (name &rest children)
  3.   (cons 'header (cons name children)))
  4.  
  5. (defun folderp (c)
  6.   (and (consp c)
  7.        (eq 'header (car c))
  8.        (symbolp (cadr c))))
  9.  
  10. (defun folder-name (c)
  11.   (when (folderp c)
  12.     (cadr c)))
  13.  
  14. (defun folder-names (cn)
  15.   (loop for c in  (children cn)
  16.         when (folderp c) collect (folder-name c)))
  17.  
  18. (defun folders (cn)
  19.   (loop for c in  (children cn)
  20.         when (folderp c) collect (identity c)))
  21.  
  22. (defun folders-find (cn name)
  23.   (loop for c in  (children cn)
  24.         when (and (folderp c)
  25.                   (eq (folder-name c) name))
  26.           collect (identity c)))
  27.  
  28. (defun folders-find-first (cn name)
  29.   (loop for c in  (children cn)
  30.         when (and (folderp c)
  31.                   (eq (folder-name c)
  32.                       name))
  33.           return (identity c)))
  34.  
  35. (defun folders-path (cn names-path)
  36.   (if (endp names-path)
  37.       cn
  38.       (let ((found (folders-find-first cn (car names-path))))
  39.         (when found
  40.           (folders-path found (cdr names-path))))))
  41.  
  42. (defun folders-make (names-path)
  43.   (if names-path
  44.       (lh (first names-path) (folders-make (rest names-path)))))
  45.  
  46. (defun folders-ensure (cn names-path)
  47.   (let ((found (folders-find-first cn (car names-path))))
  48.     (if found
  49.         ;; then
  50.         (folders-ensure found (cdr names-path))
  51.         ;; else setting the tree still does not work
  52.         (setf (cdr cn)
  53.               (append (cdr cn)
  54.                       (list (folders-make names-path)))))))
  55.  
  56. (defun children (c)
  57.   (cddr c))
  58.  
  59. (defparameter *tree* (lh 'root
  60.                          1 2
  61.                          (lh 'three
  62.                              3 33 333
  63.                              (lh 'q34
  64.                                  34
  65.                                  (lh 'q344 :a :b :c)
  66.                                  3444)
  67.                              3333 33333)
  68.                          4
  69.                          (lh 'five
  70.                              5 55 555)
  71.                          6))
  72.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement