aboutsummaryrefslogtreecommitdiff
path: root/hw02.scm
blob: 9f551a482714c2fa25bd5927d5e2e4485589d23c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#lang scheme

;; PROGRAM NAME: hw02.rkt
;; DESCRIPTION: Homework 2 for CSI 310
;; AUTHOR: Ben Burwell
;; HISTORY: Turned in 2013-01-29
;; NOTES: When this program runs, a series of expected and
;;        actual results will print out. Compare them to
;;        ensure the code functions properly.

;; ============ PROCEDURE ============
;; NAME: invert
;; 
;; DESC:
;; takes a list of 2-lists and inverts each of the lists
;; hence, the list
;;     ((2 1) (4 3) (6 5))
;; will become
;;     ((1 2) (3 4) (5 6))

(define invert
  (λ (lst)
    (cond
      [ (null? lst) null ]
      [ (not (list? lst)) (display "Invalid Parameters") ]
      [ (null? (cdr lst)) (list (invert-reverse (car lst))) ]
      [ else (append (list (invert-reverse (car lst))) (invert (cdr lst))) ] )))

;; a helper function that reverses a 2-list
;; PRECONDITION: lst is a list of the form (a b)
;; POSTCONDITION: lst is a list of the form (b a)
;; ERROR: lst is not a list with 2 elements
(define invert-reverse
  (λ (lst)
    (cond
      [ (null? lst) null ]
      [ (not (list? lst)) (display "Invalid Parameters") ]
      [ (not (equal? (length lst) 2)) (display "Invalid Parameters") ]
      [ else (list (cadr lst) (car lst)) ] )))

;; ============ TEST CODE ============
(newline)
(display "Testing (invert) =========================================")
(newline)

(display "Expected output: ()                  ")
(invert '())

(display "Expected output: ((1 2))             ")
(invert '((2 1)))

(display "Expected output: ((1 2) (3 4) (5 6)) ")
(invert '((2 1) (4 3) (6 5)))


;; ============ PROCEDURE ============
;; NAME: vector-index
;; 
;; DESC:
;; returns the zero-based index of the first 
;; occurence of a parameter in a vector, or 
;; -1 if there is no occurrence.
(define vector-index
  (λ (needle haystack)
    (cond
      [ (null? needle) (display "Invalid Parameters") ]
      [ (vector? haystack) (list-index needle (vector->list haystack)) ]
      [ else (display "Invalid Parameters") ] )))

;; a helper function
;; PRECONDITION: haystack is a non-nested list (e.g. the result of vector->list)
(define list-index
  (λ (needle haystack)
    (if (null? haystack)
        -1
        (if (equal? (car haystack) needle)
            0
            (if (equal? (list-index needle (cdr haystack)) -1)
                -1
                (+ 1 (list-index needle (cdr haystack))))))))

;; ============ TEST CODE ============
(newline)
(display "Testing (vector-index) ===================================")
(newline)

(display "Expected output: 2                   ")
(vector-index 3 #(1 2 3 4 5 6 7 8 9))

(display "Expected output: -1                  ")
(vector-index 4 #(1 2 3))

(display "Expected output: -1                  ")
(vector-index 3 #())


;; ============ PROCEDURE ============
;; NAME: count-occurrences
;;
;; DESC:
;; counts the occurrences of needle in haystack
(define count-occurrences
  (λ (needle haystack)
    (cond
      [ (null? needle) (display "Error: nothing to search for") ]
      [ (null? haystack) 0 ]
      [ (list? (car haystack)) (+ (count-occurrences needle (car haystack)) (count-occurrences needle (cdr haystack))) ]
      [ (equal? (car haystack) needle) (+ 1 (count-occurrences needle (cdr haystack))) ]
      [ else (count-occurrences needle (cdr haystack)) ] )))

;; ============ TEST CODE ============
(newline)
(display "Testing (count-occurrences) ==============================")
(newline)

(display "Expected output: 10                  ")
(count-occurrences 'a '(a b a c d (((((((((a))))))))) e f a b a a (d e) (a) c (a (a (a)))))

(display "Expected output: 0                   ")
(count-occurrences 'a '())

(display "Expected output: 1                   ")
(count-occurrences 'a '(a))

;; ============ PROCEDURE ============
;; NAME: compose
;;
;; DESC:
;; takes 1, 2, or 3 procedures and composes them, as specified by the equation:
;;     (compose f g h) = (compose f (compose g h))
(define compose
  (λ funcs
    (cond
      [ (equal? (length funcs) 1) (car funcs) ]
      [ (equal? (length funcs) 2) (λ (x) ((car funcs) ((cadr funcs) x))) ]
      [ (equal? (length funcs) 3) (λ (x) ((car funcs) ((cadr funcs) ((caddr funcs) x)))) ]
      [ else (display "Invalid parameters") ] )))

;; ============ TEST CODE ============
(newline)
(display "Testing (compose) ========================================")
(newline)

(display "Expected output: 1                   ")
((compose car cdr cdr) '(0 2 1 3))

(display "Expected output: 1                   ")
((compose car) '(1 2 3 4))

(display "Expected output: 1                   ")
((compose car cdr) '(0 1 2 3))

(display "Expected output: 1                   ")
((compose - -) 1)

;; =========== END OF FILE ===========