Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ;;new
- ;;1
- (defun plist-remove (plist prop)
- (cond ((or (null plist) (not (consp plist))) nil)
- ((eql (car plist) prop) (cddr plist))
- (t (cons (car plist) (cons (cadr plist) (plist-remove (cddr plist) prop))))))
- ;;2
- (defun plist-add (plist prop val)
- (cond ((or (null plist) (not (consp plist))) (list prop val))
- ((eql (car plist) prop) (cons prop (cons val (cddr plist))))
- (t (cons (car plist) (cons (cadr plist) (plist-add (cddr plist) prop val))))))
- ;;3
- (defun alist-remove (alist prop)
- (cond ((or (null alist) (not (consp alist))) nil)
- ((eql (caar alist) prop) (cdr alist))
- (t (cons (car alist) (alist-remove (cdr alist) prop)))))
- (defun alist-add (alist prop val)
- (cond ((or (null alist) (not (consp alist))) (list (cons prop val)))
- ((eql (caar alist) prop) (cons (cons prop val) (cdr alist)))
- (t (cons (car alist) (alist-add (cdr alist) prop val)))))
- ;;4
- ;;a
- ;;-c
- ;;--b
- ;;--d
- ;;-f
- ;;--g
- ;;---h
- ;;--g
- ;;5
- (defun tree-node (val children)
- (cons val children))
- ;;(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)))))
- ;;6
- ;;Looks at the current node and possibly its children
- (defun tree-find (tree el)
- (cond ((or (null tree) (not (consp tree))) nil)
- ((eql (car tree) el) el)
- (t (layer-find (cdr tree) el))))
- ;;Goes throught the children of the current node, if one of them contains it, it stops
- (defun layer-find (layer el)
- (cond ((or (null layer) (not (consp layer))) nil)
- (t (or (tree-find (car layer) el) (layer-find (cdr layer) el)))))
- ;;7
- (defun tree-sum (tree)
- (cond ((or (null tree) (not (consp tree))) 0)
- ((numberp (car tree)) (+ (car tree) (tree-sum (cdr tree))))
- ((consp (car tree)) (+ (tree-sum (car tree)) (tree-sum (cdr tree))))
- (t (tree-sum (cdr tree)))))
- ;;8
- ;;Who knows/cares how to do this one? :)
- ;;9
- (defun tree-height (tree)
- (cond ((or (null tree) (not (consp tree))) 0)
- (t (+ 1 (layer-height (cdr tree) 0)))))
- (defun layer-height (layer max)
- (cond ((or (null layer) (not (consp layer))) max)
- (t (layer-height (cdr layer) (tree-height (car layer))))))
- ;;10
- ;;(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)))
- ;;11
- ;;(my-adjoin 8 (my-adjoin 6 (my-adjoin 7 (my-adjoin 4 (my-adjoin 3 (my-adjoin 1 (my-adjoin 2 (my-adjoin 5 ()))))))))
- ;;(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