Subversion Repositories programming

Rev

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

Rev Author Line No. Line
66 irasnyd 1
irasnyd@duallie lisp $ cat hw07.lisp
2
;Written By: Ira Snyder
3
;Due Date:   02-11-2005
4
;Homework #: hw07
5
 
6
;;; The next two functions define a special insertion sort
7
;;; that will sort a Polynomial-Normal-Form list
8
(defun ISORT (L)
9
  (cond
10
    ((null L) nil)
11
    (t        (ISORT1 (car L) (ISORT (cdr L))))
12
  )
13
)
14
 
15
;;; Helper for ISORT
16
(defun ISORT1 (N L)
17
  (cond
18
    ((null L)       (list N))
19
    ((>= (cadr N) (cadar L)) (cons N L))
20
    (t              (cons (car L) (ISORT1 N (cdr L))))
21
  )
22
)
23
 
24
;;; Combines the coefficients of a two part list, L1, 
25
;;; and a list comprised of two part lists, L2
26
;;; Example: L1 <- (1 1)
27
;;;          L2 <- ((2 2) (3 3) (4 4) (1 1))
28
;;; Returns: (2 1)
29
;;;
30
;;; It is good for combining the parts of a PNF expression
31
(defun COMBINE (L1 L2)
32
  (cond
33
    ((null L2)                L1)
34
    ((= (cadr L1) (cadar L2)) (COMBINE (list (+ (car L1) (caar L2)) (cadr L1))
35
                                       (cdr L2)))
36
    (t                        (COMBINE L1 (cdr L2)))
37
  )
38
)
39
 
40
;;; Deletes any two part list contained within the
41
;;; list L with the exponent given as an argument
42
;;; Example: EXP <- 1
43
;;;          L <- ((1 2) (1 1) (3 1) (1 4))
44
;;; Returns: ((1 2) (1 4))
45
(defun DEL-EXP (EXP L)
46
  (cond
47
    ((null L)          nil)
48
    ((= EXP (cadar L)) (DEL-EXP EXP (cdr L))) ;remove an element
49
    (t                 (cons (car L) (DEL-EXP EXP (cdr L))))
50
  )
51
)
52
 
53
;;; Combines all like terms in a PNF expression, reducing it
54
;;; completely. It does NOT sort the resulting list however.
55
(defun MY-REDUCE (L)
56
  (cond
57
    ((null L) nil)
58
    (t        (cons (COMBINE (car L) (cdr L)) 
59
                    (MY-REDUCE (DEL-EXP (cadr (COMBINE (car L) (cdr L))) L))))
60
  )
61
)
62
 
63
(defun DEL-ZERO (L)
64
  (cond
65
    ((null L) nil)
66
    ((= 0 (caar L)) (DEL-ZERO (cdr L)))
67
    (t              (cons (car L) (DEL-ZERO (cdr L))))
68
  )
69
)
70
 
71
;;; This takes a non-reduced, unsorted list representing a
72
;;; PNF expression, and reduces and sorts it into correct
73
;;; Polynomial-Normal-Form.
74
(defun PNF (L) (ISORT (DEL-ZERO (MY-REDUCE L))))
75
 
76
irasnyd@duallie lisp $ cat hw07test.lisp
77
; Test cases for CS 352, Winter 2005, Homework 7.
78
 
79
(defun TEST nil
80
  (TEST1 nil)
81
  (TEST1 '((1 1)))
82
  (TEST1 '((0 1)))
83
  (TEST1 '((1 1)(1 1)))
84
  (TEST1 '((1 1)(-1 1)))
85
  (TEST1 '((2 0)(3 1)(4 2)))
86
  (TEST1 '((4 2)(3 1)(2 0)))
87
  (TEST1 '((4 2)(3 1)(2 0)(-3 1)))
88
  (TEST1 '((3 1)(2 1)(-4 1)))
89
  (TEST1 '((1 2)(-5 0)(2 2)(-3 1)(5 0)(-3 2)(3 1)))
90
  (TEST1 '((1 2)(2 0)(2 1)(2 3)(3 1)(7 0)))
91
)
92
 
93
(defun TEST1 (TERMS)
94
  (format t "~%in  ~s~%" TERMS)
95
  (format t "out ~s~%" (PNF TERMS))
96
)
97
 
98
irasnyd@duallie lisp $ clisp -q
99
 
100
[1]> (load 'hw07.lisp)
101
;; Loading file hw07.lisp ...
102
;; Loaded file hw07.lisp
103
T
104
[2]> (load 'hw07test.lisp)
105
;; Loading file hw07test.lisp ...
106
;; Loaded file hw07test.lisp
107
T
108
[3]> (test)
109
 
110
in  NIL
111
out NIL
112
 
113
in  ((1 1))
114
out ((1 1))
115
 
116
in  ((0 1))
117
out NIL
118
 
119
in  ((1 1) (1 1))
120
out ((2 1))
121
 
122
in  ((1 1) (-1 1))
123
out NIL
124
 
125
in  ((2 0) (3 1) (4 2))
126
out ((4 2) (3 1) (2 0))
127
 
128
in  ((4 2) (3 1) (2 0))
129
out ((4 2) (3 1) (2 0))
130
 
131
in  ((4 2) (3 1) (2 0) (-3 1))
132
out ((4 2) (2 0))
133
 
134
in  ((3 1) (2 1) (-4 1))
135
out ((1 1))
136
 
137
in  ((1 2) (-5 0) (2 2) (-3 1) (5 0) (-3 2) (3 1))
138
out NIL
139
 
140
in  ((1 2) (2 0) (2 1) (2 3) (3 1) (7 0))
141
out ((2 3) (1 2) (5 1) (9 0))
142
NIL
143
[4]> (bye)
144
irasnyd@duallie lisp $