-
Notifications
You must be signed in to change notification settings - Fork 0
/
simply_scheme.rkt
159 lines (107 loc) · 3.27 KB
/
simply_scheme.rkt
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
157
158
#lang simply-scheme
; lambda
(define (add-three number)
(+ number 3))
(define (add-three-to-each sent)
(every add-three sent))
(add-three-to-each (1992))
;use lambda to generate a procedure that does those things for us
(define (add-three-to-each sent)
(every (lambda (number) (+ number 3)) sent))
(add-three-to-each (1 9 9 2))
((lambda (a b) (+ (* 2 a) b)) 5 6)
((lambda (wd) (word (last wd) (first wd))) 'impish)
(define (acronym phrase)
(accumulate word (every first phrase)))
(acronym '(american civil liberties union))
(acronym '(reduced instruction set computer))
(acronym '(quod erat demonstrandum))
(first 'american)
(every first '(american civil liberties union))
(accumulate word '(a c l u))
(acronym '(united states of america))
(define (acronym phrase)
(accumulate word (every first (keep real-word? phrase))))
(define (real-word? wd)
(not (member? wd '(a the an in of and for to with))))
(acronym '(united states of america))
(acronym '(structure and interpretation of computer programs))
(acronym '(association for computing machinery))
(real-word? 'structure)
(real-word? 'of)
(keep real-word? '(united network command for law and enforcement))
(define (pigl wd)
(if (member? (first wd) 'aeiou)
(word wd 'ay)
(pigl (word (butfirst wd) (first wd)))))
(pigl 'spaghetti)
(pigl 'ok)
(pigl 'elphant)
(first 'spaghetti)
(butfirst 'spaghetti)
(word 'paghetti 's)
(define (rotate wd)
(word (butfirst wd) (first wd)))
(rotate 'spaghetti)
(rotate 'paghettis)
(pigl 'aghettisp)
(every pigl '(the ballad of john and yoko))
;;;;;;
; simple racket program for menu population
(define (choices menu)
(if (null? menu)
'(())
(let ((smaller (choices (cdr menu))))
(reduce append
(map (lambda (item) (prepend-every item smaller))
(car menu))))))
(define (prepend-every item 1st)
(map (lambda (choice) (se item choice)) 1st))
(choices '((small medium large)
(vanilla (ultra chocolate) (rum raisin) ginger)
(cone cup)))
;result:
'((small vanilla cone)
(small vanilla cup)
(small ultra chocolate cone)
(small ultra chocolate cup)
(small rum raisin cone)
(small rum raisin cup)
(small ginger cone)
(small ginger cup)
(medium vanilla cone)
(medium vanilla cup)
(medium ultra chocolate cone)
(medium ultra chocolate cup)
(medium rum raisin cone)
(medium rum raisin cup)
(medium ginger cone)
(medium ginger cup)
(large vanilla cone)
(large vanilla cup)
(large ultra chocolate cone)
(large ultra chocolate cup)
(large rum raisin cone)
(large rum raisin cup)
(large ginger cone)
(large ginger cup))
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;expressing complex techniques simply in scheme
(define (combinations size set)
(cond ((= size 0) '(()))
((empty? set) '())
(else (append (prepend-every (first set)
(combinations (- size 1)
(butfirst set)))
(combinations size (butfirst set))))))
(combinations 3 '(a b c d e))
(combinations 2 '(john paul george ringo))
;;;;;;;;;;;;;;;;;;
(define (factorial n)
(if (= n 0)
1
(* n (factorial (- n 1)))))
(factorial 4)
(factorial 1000)
(factorial 400)
;;;;;;;;;;;;;;;;;;