Rev 430 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
; HW04.lisp - Homework Assignment #4 for CS408, Fall Quarter, 2006
; Copyright (c) 2006 Ira W. Snyder (devel@irasnyder.com)
;
; Permission is hereby granted, free of charge, to any person obtaining a copy
; of this software and associated documentation files (the "Software"), to deal
; in the Software without restriction, including without limitation the rights
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
; copies of the Software, and to permit persons to whom the Software is
; furnished to do so, subject to the following conditions:
;
; The above copyright notice and this permission notice shall be included in all
; copies or substantial portions of the Software.
;
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
; SOFTWARE.
;;; Problem 1
;;;
;;; The example output given in the homework assignment page is incorrect,
;;; according to the description. The description states that we should keep
;;; anything that is strictly less than LO, and strictly higher than HI. The
;;; example output for (list_outliers 3 10 '(3 5 1 9 11 30)) contains 3, which
;;; is incorrect, since it is not strictly less than LO, which is 3.
(defun LIST_OUTLIERS (LO HI LI)
(cond
((null LI) nil)
((or (< LO (car LI)) (> HI (car LI))) (cons (car LI) (LIST_OUTLIERS LO HI (cdr LI))))
(t (LIST_OUTLIERS LO HI (cdr LI)))
)
)
;;; Problem 2
(defun dolist_reverse (LI)
(let ((RES nil))
(dolist (EL LI RES)
(setq RES (cons EL RES))
)
)
)
; The real way I'd do it, using a do-loop instead of dolist
(defun DO_REVERSE (LI)
(do ((RES nil (cons (car LI) RES))
(LI LI (cdr LI)))
((null LI) RES)
)
)
;;; Problem 3
(defun DO_FACTORIAL (NUM)
(do ((NUM NUM (1- NUM))
(RES 1 (* RES NUM)))
((<= NUM 0) RES)
)
)
;;; Problem 4
(defun DO_MEMBER (EL LI)
(do ((LI LI (cdr LI)))
((or (null LI) (equal (car LI) EL)) LI)
)
)
; vim: set ts=2 sts=2 sw=2 expandtab textwidth=80: