430 |
ira |
1 |
; HW04.lisp - Homework Assignment #4 for CS408, Fall Quarter, 2006
|
|
|
2 |
; Copyright (c) 2006 Ira W. Snyder (devel@irasnyder.com)
|
|
|
3 |
;
|
|
|
4 |
; Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
5 |
; of this software and associated documentation files (the "Software"), to deal
|
|
|
6 |
; in the Software without restriction, including without limitation the rights
|
|
|
7 |
; to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
8 |
; copies of the Software, and to permit persons to whom the Software is
|
|
|
9 |
; furnished to do so, subject to the following conditions:
|
|
|
10 |
;
|
|
|
11 |
; The above copyright notice and this permission notice shall be included in all
|
|
|
12 |
; copies or substantial portions of the Software.
|
|
|
13 |
;
|
|
|
14 |
; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
15 |
; IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
16 |
; FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
17 |
; AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
18 |
; LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
19 |
; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
|
20 |
; SOFTWARE.
|
|
|
21 |
|
|
|
22 |
;;; Problem 1
|
|
|
23 |
;;;
|
|
|
24 |
;;; The example output given in the homework assignment page is incorrect,
|
|
|
25 |
;;; according to the description. The description states that we should keep
|
|
|
26 |
;;; anything that is strictly less than LO, and strictly higher than HI. The
|
432 |
ira |
27 |
;;; example output for (list_outliers 10 3 '(3 5 1 9 11 30)) contains 3, which
|
430 |
ira |
28 |
;;; is incorrect, since it is not strictly less than LO, which is 3.
|
432 |
ira |
29 |
(defun LIST_OUTLIERS (HI LO LI)
|
430 |
ira |
30 |
(cond
|
|
|
31 |
((null LI) nil)
|
432 |
ira |
32 |
((or (< (car LI) LO) (> (car LI) HI)) (cons (car LI) (LIST_OUTLIERS HI LO (cdr LI))))
|
|
|
33 |
(t (LIST_OUTLIERS HI LO (cdr LI)))
|
430 |
ira |
34 |
)
|
|
|
35 |
)
|
|
|
36 |
|
|
|
37 |
;;; Problem 2
|
|
|
38 |
(defun dolist_reverse (LI)
|
|
|
39 |
(let ((RES nil))
|
|
|
40 |
(dolist (EL LI RES)
|
|
|
41 |
(setq RES (cons EL RES))
|
|
|
42 |
)
|
|
|
43 |
)
|
|
|
44 |
)
|
|
|
45 |
|
|
|
46 |
; The real way I'd do it, using a do-loop instead of dolist
|
|
|
47 |
(defun DO_REVERSE (LI)
|
|
|
48 |
(do ((RES nil (cons (car LI) RES))
|
|
|
49 |
(LI LI (cdr LI)))
|
|
|
50 |
((null LI) RES)
|
|
|
51 |
)
|
|
|
52 |
)
|
|
|
53 |
|
|
|
54 |
;;; Problem 3
|
|
|
55 |
(defun DO_FACTORIAL (NUM)
|
|
|
56 |
(do ((NUM NUM (1- NUM))
|
|
|
57 |
(RES 1 (* RES NUM)))
|
|
|
58 |
((<= NUM 0) RES)
|
|
|
59 |
)
|
|
|
60 |
)
|
|
|
61 |
|
|
|
62 |
;;; Problem 4
|
|
|
63 |
(defun DO_MEMBER (EL LI)
|
|
|
64 |
(do ((LI LI (cdr LI)))
|
|
|
65 |
((or (null LI) (equal (car LI) EL)) LI)
|
|
|
66 |
)
|
|
|
67 |
)
|
|
|
68 |
|
|
|
69 |
; vim: set ts=2 sts=2 sw=2 expandtab textwidth=80:
|