(defun make-bstree-node (key data left right) (list '*bstree* key data left right))The first symbol makes your predicate easy to define:
(defun bstree-p (node) (eq (car node) '*bstree*))Now you can write accessor functions for each of these, as follows:
(defun bstree-node-key (node) (cadr node)) (defun bstree-node-data (node) (caddr node)) (defun bstree-node-left (node) (cadddr node)) (defun bstree-node-right (node) (nth 4 node)))You could even write destructive modifiers such as:
(defun set-bstree-node-left (node left-kid) (setf (cadddr node) left-kid))Which would set the left kid of node to be the new left-kid input. Alternatively, one could use the def-struct macro, which creates a record-style data structure for you, giving you constructors, accessors and a predicate for free. Consult the manual for further details on how to use defstruct.
(let (binding-1 ... binding-n) lisp-expressions)where each binding is of the form:
(variable-name value)And value could be any lisp function returning a value, while lisp-expressions is a sequence of expressions. Within lisp-expression, any occurance of variable-name will evaluate to strong>value (unless of course variable-name is rebound by another let command). One can also embed function definitions (defun calls) within a let statement, and then the function has access to the variable, which maintains its value between function calls (though no functions outside the let statement have access to this value, except by calling one of these functions).
[cardinality {set elements} level]Though for part II, since all sets are of level 1, we could ignore the level. These functions should work as long as your predicate setp and your three accessors cardinality, elements, and level work. This printing operation does not make any assumptions about how you represent sets. You do not have to try to make your set representation efficient for printing - this is just for us to tell what sets you construct. Also, it is only printing the elements in the order that your elements function returns them, there is no need to correlate this with any internally sorted order. The sample data output will be shown in this format. These files can also be found in the dt01 course account in file print-set.lisp .
;this function prints a simple set in the format: ; (makeset '(a b)) --> [2 {A B}] ;note this will not work properly for higher-level sets. (defun princ-s-set (set) (cond ((setp set) (princ "[") (princ (cardinality set)) (princ " ") (princ "{") (let ((elts (elements set))) (and elts (princ (car (elements set))) (mapcar (function (lambda (x) (princ " ")(princ x))) (cdr (elements set))))) (princ "}") (princ "]") ) (t (princ set))) set) ;this function prints a higher level set in the format: ; (makeset '(a b)) --> [2 {A B} 1] (defun princ-h-set (set) (cond ((setp set) (princ "[") (princ (cardinality set)) (princ " ") (princ "{") (let ((elts (elements set))) (and elts (princ-h-set (car (elements set))) (mapcar (function (lambda (x) (princ " ")(princ-h-set x))) (cdr (elements set))))) (princ "}") (princ " ") (princ (level set)) (princ "]") ) (t (princ set))) set)Note that this will not allow you to type in sets in this format to lisp. You will have to use constructor functions like make-set to input sets. If you wanted to be able to just type in sets like the output function, you'd have to write a reader. You can write one that accepts the same format (and builds the valid set, calling make-set), for 5 points of extra credit, or perhaps your kindly TA might write one for you if he's feeling generous, with some spare time.
(defun Union-from (set i n) (cond ((> i n) (Nullset)) (t (set-union (powerset (rem-element (nth-item i set) set)) (union-from set (1+ i) n))))) (defun power-set (set) (set-union (make-set set) (union-from set 1 (cardinality set))))For higher-level sets, we will have not just one universe, but one universe for each level. In general, U^0 is U, and for n>0, U^n is defined as setunion(U^(n-1),Powerset(U^(n-1))). Remember that complements of higher-order sets of level n, are to be with respect to U^(n-1).