; standard bubblesort, making N passes through a list of N elements, ; swapping out-of-sequence adjacent elements on each pass ; expects a list, L, ; a function to check an element is of the correct type, e.g. 'integerp ; and a function to check if two elements are in the wrong order in the list, e.g. '< (defun bubblesort (typeCheckerF outOfOrderF L) (if (and (listp L) (> (length L) 1)) (dotimes (passNum (length L) passNum) (dotimes (epos (- (length L) 1) epos) (let ((first (nth epos L)) (second (nth (+ 1 epos) L))) (if (and (funcall typeCheckerF first) (funcall typeCheckerF second) (funcall outOfOrderF first second)) (block swapFirstAndSecond (setf (nth epos L) second) (setf (nth (+ 1 epos) L) first)))))))) ; bubblesorts given list whose elements are strings/lists, sorts the list by element lengths (defun SLsorter (SL) (bubblesort (lambda (x) (or (listp x) (stringp x))) (lambda (a b) (> (length a) (length b))) SL)) ; sample call (defvar S '("hi" "there" (10 20 30 40) "bob" nil "bye" "?")) (SLsorter S) |
(defun myReduce (f x &rest R) (cond ((null R) x) (t (apply 'myRed (cons f (cons (funcall f x (car R)) (cdr R))))))) (format t "~A~%" (myReduce '/ 120)) ; gives 120 (format t "~A~%" (myReduce '/ 120 3)) ; gives 40 (format t "~A~%" (myReduce '/ 120 3 0.5)) ; gives 80 (format t "~A~%" (myReduce '/ 120 3 0.5 -5)) ; gives -16 |
(defun stackAccess (L) (let ((elements nil)) (if (listp L) (setf elements (copy-list L))) (lambda (cmd &optional (e nil)) (cond ((equalp cmd 'top) (if (not (null elements)) (car elements) nil)) ((equalp cmd 'pop) (if (not (null elements)) (setf elements (cdr elements)))) ((equalp cmd 'size) (length elements)) ((equalp cmd 'push) (setf elements (cons e elements))) (t (format t "Invalid command ~A~%" cmd)))))) |
(defmacro myReduce (f x &rest R) (cond ((null R) x) ((null (cdr R)) (list f x (car R))) (t `(myReduce ,f (,f ,x ,(car R)) ,@(cdr R))))) |
hint: if given just x then it's true iff x is true if given x and 'the rest' then it's true if either x is true and all the rest are false or x is false and exactly one of the rest is true