Advertisement
Nuparu00

new new new new new new new new new new lisp

Nov 8th, 2021
3,458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lisp 2.72 KB | None | 0 0
  1. ;;new
  2.  
  3. ;;1
  4. (defun plist-remove (plist prop)
  5.   (cond ((or (null plist) (not (consp plist))) nil)
  6.         ((eql (car plist) prop) (cddr plist))
  7.         (t (cons (car plist) (cons (cadr plist) (plist-remove (cddr plist) prop))))))
  8.  
  9. ;;2
  10. (defun plist-add (plist prop val)
  11.   (cond ((or (null plist) (not (consp plist))) (list prop val))
  12.         ((eql (car plist) prop) (cons prop (cons val (cddr plist))))
  13.         (t (cons (car plist) (cons (cadr plist) (plist-add (cddr plist) prop val))))))
  14.  
  15. ;;3
  16.  
  17. (defun alist-remove (alist prop)
  18.   (cond ((or (null alist) (not (consp alist))) nil)
  19.         ((eql (caar alist) prop) (cdr alist))
  20.         (t (cons (car alist) (alist-remove (cdr alist) prop)))))
  21.  
  22. (defun alist-add (alist prop val)
  23.   (cond ((or (null alist) (not (consp alist))) (list (cons prop val)))
  24.         ((eql (caar alist) prop) (cons (cons prop val) (cdr alist)))
  25.         (t (cons (car alist) (alist-add (cdr alist) prop val)))))
  26.        
  27. ;;4
  28.  
  29. ;;a
  30. ;;-c
  31. ;;--b
  32. ;;--d
  33. ;;-f
  34. ;;--g
  35. ;;---h
  36. ;;--g
  37.  
  38. ;;5
  39.  
  40. (defun tree-node (val children)
  41.   (cons val children))
  42.  
  43. ;;(tree-node 5 (list (tree-node 2 (list (tree-node 1 nil) (tree-node 3 (list (tree-node 4 nil))))) (tree-node 7 (list (tree-node 6 nil) (tree-node 8 nil)))))
  44.  
  45. ;;6
  46.  
  47. ;;Looks at the current node and possibly its children
  48. (defun tree-find (tree el)
  49.   (cond ((or (null tree) (not (consp tree))) nil)
  50.         ((eql (car tree) el) el)
  51.         (t (layer-find (cdr tree) el))))
  52.  
  53. ;;Goes throught the children of the current node, if one of them contains it, it stops
  54. (defun layer-find (layer el)
  55.   (cond ((or (null layer) (not (consp layer))) nil)
  56.         (t (or (tree-find (car layer) el) (layer-find (cdr layer) el)))))
  57.  
  58. ;;7
  59. (defun tree-sum (tree)
  60.   (cond ((or (null tree) (not (consp tree))) 0)
  61.         ((numberp (car tree)) (+ (car tree) (tree-sum (cdr tree))))
  62.         ((consp (car tree)) (+ (tree-sum (car tree)) (tree-sum (cdr tree))))
  63.         (t (tree-sum (cdr tree)))))
  64.  
  65. ;;8
  66. ;;Who knows/cares how to do this one? :)
  67.  
  68.  
  69. ;;9
  70. (defun tree-height (tree)  
  71.   (cond ((or (null tree) (not (consp tree))) 0)
  72.         (t (+ 1 (layer-height (cdr tree) 0)))))
  73.  
  74. (defun layer-height (layer max)
  75.   (cond ((or (null layer) (not (consp layer))) max)
  76.         (t (layer-height (cdr layer) (tree-height (car layer))))))
  77.  
  78. ;;10
  79. ;;(binary-tree-node 5 (binary-tree-node 2 (binary-tree-node 1 nil nil) (binary-tree-node 3 nil (binary-tree-node 4 nil nil))) (binary-tree-node 7 (binary-tree-node 6 nil nil) (binary-tree-node 8 nil nil)))
  80.  
  81. ;;11
  82. ;;(my-adjoin 8 (my-adjoin 6 (my-adjoin 7 (my-adjoin 4 (my-adjoin 3 (my-adjoin 1 (my-adjoin 2 (my-adjoin 5 ()))))))))
  83. ;;(my-adjoin 8 (my-adjoin 6 (my-adjoin 7 (my-adjoin 3 (my-adjoin 4 (my-adjoin 1 (my-adjoin 2 (my-adjoin 5 ()))))))))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement