Subversion Repositories programming

Rev

Rev 431 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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
27
;;; example output for (list_outliers 3 10 '(3 5 1 9 11 30)) contains 3, which
28
;;; is incorrect, since it is not strictly less than LO, which is 3.
29
(defun LIST_OUTLIERS (LO HI LI)
30
  (cond
31
    ((null LI)                            nil)
32
    ((or (< LO (car LI)) (> HI (car LI))) (cons (car LI) (LIST_OUTLIERS LO HI (cdr LI))))
33
    (t                                    (LIST_OUTLIERS LO HI (cdr LI)))
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
;;; FIXME: not finished
56
(defun DO_FACTORIAL (NUM)
57
  (do ((NUM NUM (1- NUM))
58
       (RES 1 (* RES NUM)))
59
      ((<= NUM 0) RES)
60
  )
61
)
62
 
63
;;; Problem 4
64
(defun DO_MEMBER (EL LI)
65
  (do ((LI LI (cdr LI)))
66
      ((or (null LI) (equal (car LI) EL)) LI)
67
  )
68
)
69
 
70
; vim: set ts=2 sts=2 sw=2 expandtab textwidth=80: