;;;;More abour parameters ;Adam Nelson ;Chet Tobrey (setf (symbol-function 'find-all-if) #'remove-if-not) ;;;; this function is defined in the version of ANSI Common Lisp we are using;;;; #|(defun complement (fn) "if FN returns y, then (complement FN) returns (not y)" #'(lambda (&rest args) (not (apply fn args))))|# (defun find-all (item sequence &rest keyword-args &key (test #'eql) test-not &allow-other-keys) "Find all those elements of sequence that match item according to the keywords. Doesn't alter sequence." (if test-not (apply #'remove item sequence :test-not (complement test-not) keyword-args) (apply #'remove item sequence :test (complement test) keyword-args))) (egs :3.19 (eg '(defun math-quiz (op range n) "Ask the user a series of math problems." (dotimes (i n) (problem (random range) op (random range)))) :of "defining the naive math-quiz" :out 'MATH-QUIZ) (eg '(defun problem (x op y) "Ask a math problem, read reply, and say if correct" (format t "~&How Much is ~d ~a ~d?" x op y) (if (eql (read) (funcall op x y)) (princ "Correct!") (princ "Sorry incorrect :("))) :of "problem function used in math quiz" :out 'PROBLEM) (eg '(math-quiz '+ 100 2) :of "math quiz in use") (eg '(defun math-quiz-two (&optional (op '+)(range 100)(n 2)) "asks the user a series of math problems" (dotimes (i n) (problem (random range) op (random range)))) :of "same function but with optional parameters. default values are specified if no params are given") (eg '(math-quiz-two) :of "math quiz two") (eg '(defun math-quiz-three (&key (op '+)(range 100)(n 2)) "Math quiz" (dotimes (i n) (problem (random range) op (random range)))) :of "the &key argument allows us to specify arguments.&key, &optional, and &rest are called lambda-list keywords.") (eg '(math-quiz-three :op '-) :of "using :op '-") (eg '(defun times-two (&xyz) (+ &xyz &xyz)) :of "& has no significance and is treated as a symbol." :out 'TIMES-TWO) (eg '(times-two 3) :of "using f" :out '6) (eg '(defun g (&key x y)(list x y)) :of "function g" :out 'G) (eg '(let ((keys '(:x :y :z))) (g (second keys) 1 (first keys)2)) :of "specifying the keys and where they will end up in the list" :out '(2 1)) (eg '(find 3 '(1 2 3 4 -5 6.0)) :of "find looks for a particular element in a sequence" :out '3) (eg '(find 6 '(1 2 3 4 -5 6.0)) :of "looking for 6" :out '6) (eg '(find 6 '(1 2 3 4 -5 6.0) :test #'equalp) :of "find takes several keyword arguments. if none is specified it tests with eql. here we use equalp" :out '6.0) (eg '(setf nums '(1 2 3 2 1)) :of "sets nums to the list (1 2 3 2 1)" :out '(1 2 3 2 1)) (eg '(find-all 1 nums :test #'=) :of "checks all of the numbers in the list to a value of 1" :out '(1 1)) (eg '(find-all 1 nums :test #'= :key #'abs) :of "removing 1's" :out '(1 1)) )