Subversion Repositories programming

Rev

Rev 72 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
72 irasnyd 1
irasnyd@duallie lisp $ cat hw12.lisp
2
;Written By: Ira Snyder
3
;Due Date:   02-28-2005
4
;Homework #: HW12
5
 
6
;;; This is the generic implementation of the recursive schema OP-SOME.
7
;;; It takes 3 parameters:
8
;;;   CONDITION: when this is true, the operation happens on that element
9
;;;   OP: the operation to happen on the values that satisfy CONDITION
10
;;;   ARG: the list of items on which to operate
11
;;;
12
;;; Example: (OP-SOME #'oddp #'sq '(1 2 3 4 5)) -> (1 2 9 4 25)
13
(defun OP-SOME (CONDITION OP ARG)
14
  (cond
15
    ((null ARG)                    nil)
16
    ((funcall CONDITION (car ARG)) (cons (funcall OP (car ARG)) 
17
                                         (OP-SOME CONDITION OP (cdr ARG))))
18
    (t                             (cons (car ARG) 
19
                                         (OP-SOME CONDITION OP (cdr ARG))))
20
  )
21
)
22
 
23
;;; Returns the square of the number passed in as a parameter.
24
;;; This is to aid in testing of OP-SOME
25
(defun SQ (NUM)
26
  (* NUM NUM)
27
)
28
 
29
irasnyd@duallie lisp $ clisp -q
30
 
31
[1]> (load 'hw12.lisp)
32
;; Loading file hw12.lisp ...
33
;; Loaded file hw12.lisp
34
T
35
[2]> (OP-SOME #'oddp #'SQ nil)
36
NIL
37
[3]> (OP-SOME #'oddp #'SQ '(2))
38
(2)
39
[4]> (OP-SOME #'oddp #'SQ '(3))
40
(9)
41
[5]> (OP-SOME #'oddp #'SQ '(3 6 4 5 2))
42
(9 6 4 25 2)
43
[6]> (bye)
44
irasnyd@duallie lisp $ 
45