summaryrefslogtreecommitdiffstats
path: root/final_project/work/generic-string-opers.scm
blob: 9855793be772797631b68cfd56c0524b2bac9281 (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

; STRING
(define capitalize (make-generic-operator 1 #f 'capitalize))
(define downcase (make-generic-operator 1 #f 'downcase))
(define upcase (make-generic-operator 1 #f 'upcase))
(define string-length (make-generic-operator 1 #f 'length))
(defhandler capitalize (lambda (x) (string-capitalize x)) string?)
(defhandler downcase (lambda (x) (string-downcase x)) string?)
(defhandler upcase (lambda (x) (string-upcase x)) string?)
(defhandler string-length (lambda (x) (string-length x)) string?)

; STRING STRING
(define search-forward (make-generic-operator 2 #f 'search-forward))
(define search-backward (make-generic-operator 2 #f 'search-backward))
(define search-all (make-generic-operator 2 #f 'search-all))
(define is-substring (make-generic-operator 2 #f 'substring?))
(define match (make-generic-operator 2 #f 'match))
(defhandler match (lambda (x y) (string-match-forward x y)) string? string?)
(defhandler search-forward (lambda (x y) (string-search-forward x y)) string? string?)
(defhandler search-all (lambda (x y) (string-search-all x y)) string? string?)
(defhandler search-backward (lambda (x y) (string-search-backward x y)) string? string?)
(defhandler is-substring (lambda (x y) (substring? x y)) string? string?)

; NUMBER NUMBER
(define plus (make-generic-operator 2 #f '+))
(define subtract (make-generic-operator 2 #f '-))
(define mul (make-generic-operator 2 #f '*))
(define divide (make-generic-operator 2 #f '/))
(define are-equal (make-generic-operator 2 #f '=?))
(defhandler plus (lambda (x y) (+ x y)) number? number?)
(defhandler subtract (lambda (x y) (- x y)) number? number?)
(defhandler mul (lambda (x y) (* x y)) number? number?)
(defhandler divide (lambda (x y) (/ x y)) number? number?)
(defhandler are-equal (lambda (x y) (= x y)) number? number?)

; LIST
(define length2 (make-generic-operator 1 #f 'length))
(define reverse1 (make-generic-operator 1 #f 'reverse))
(define sort1 (make-generic-operator 1 #f 'sort))
(defhandler length2 (lambda (x) (length x)) list?)
(defhandler reverse1 (lambda (x) (reverse x)) list?)
(defhandler sort1 (lambda (x) (sort x <)) list?)

; BOOLEAN
(define not1 (make-generic-operator 1 #f 'not))
(defhandler not1 (lambda (x) (not x)) boolean?)