-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgramsSkimScheme.txt
195 lines (146 loc) · 5.02 KB
/
ProgramsSkimScheme.txt
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
; PROGRAMA 1 - Quicksort: usa o null?, append, cons, if, not, lt?
; String: "(begin (define quicksort (lambda (L) (if (null? L) '() (append (quicksort (list< (car L) (cdr L))) (cons (car L) (quicksort (list>= (car L) (cdr L)))))))) (define list< (lambda (a b) (if (null? b) '() (if ( lt? a (car b)) (list< a (cdr b)) (cons (car b) (list< a (cdr b))) )))) (define list>= (lambda (a b) (if (null? b) '() (if (not ( lt? a (car b))) (list>= a (cdr b)) (cons (car b) (list>= a (cdr b))) )))) (define result (quicksort '(5 3 2 6 1))) result)"
(begin
(define quicksort
(lambda (L)
(if (null? L)
'()
(append (quicksort (list< (car L) (cdr L)))
(cons (car L) (quicksort (list>= (car L) (cdr L))))
)
)
)
)
(define list<
(lambda (a b)
(if (null? b)
'()
(if ( lt? a (car b))
(list< a (cdr b))
(cons (car b) (list< a (cdr b)))
)
)
)
)
(define list>=
(lambda (a b)
(if (null? b)
'()
(if (not ( lt? a (car b)))
(list>= a (cdr b))
(cons (car b) (list>= a (cdr b)))
)
)
)
)
(define result (quicksort '(5 3 2 6 1)))
result
)
; PROGRAMA 2 - Fibonacci: usa o if, eqv? e recursão
; String: "(begin (define fib2 (lambda (x) (if (eqv? x 0) 0 (if (eqv? x 1) 1 (+ (fib2 (- x 1)) (fib2 (- x 2))))))) (define result (fib2 8) ) result)"
(begin
(define fib2
(lambda (x)
(if (eqv? x 0)
0
(if (eqv? x 1)
1
(+ (fib2 (- x 1)) (fib2 (- x 2)))
)
)
)
)
(define result (fib2 8))
result
)
; PROGRAMA 3 - Media: usa o div
; String: "(begin (define media (lambda (x y z a b) (/ (+ x y z a b ) 5) )) (define result (media 2 3 4 5 3)) result)"
(begin
(define media
(lambda (x y z a b)
(/ (+ x y z a b ) 5)
)
)
(define result (media 2 3 4 5 3))
result
)
; PROGRAMA 4 - MDC: usa o modulo, eq?, e o if
; String: "(begin (define mdcMod (lambda (x y) (if (eqv? x 0) y (if (eqv? y 0) x (if (> x y) (mdcMod (modulo x y) y) (mdcMod (modulo y x) x)))))) (define result (mdcMod 40 8)) result)"
(begin
(define mdcMod
(lambda (x y)
(if (eqv? x 0)
y
(if (eqv? y 0)
x
(if (> x y)
(mdcMod (modulo x y) y)
(mdcMod (modulo y x) x)
)
)
)
)
)
(define result (mdcMod 40 8))
result
)
; PROGRAMA 5 - let: usa o let e variáveis locais
; String: "(begin (define x 4) (let ((x 2) (y 10)) (+ x y) (set! x 33) x))"
(begin
(define x 4)
(let
((x 2) (y 10))
(+ x y)
(set! x 33)
x
)
)
; PROGRAMA 6 - set:
; String: "(begin (define x 4) (set! x 10) (set! y 5))"
(begin
(define x 4)
(set! x 10)
(set! y 5)
)
; PROGRAMA 7 - divisão por zero
; String: "(begin (define x 4) (define y 0) (/ x y))"
(begin
(define x 4)
(define y 0)
(/ x y)
)
; PROGRAMA 8 - testa create-struct, set-attr! e get-attr:
; String: "(begin (define estrutura (create-struct '(atributo1 atributo2 atributo3 atributo4))) (set! estrutura (set-attr! estrutura atributo1 \"Nós \")) (set! estrutura (set-attr! estrutura atributo2 \"amamos \")) (set! estrutura (set-attr! estrutura atributo3 \"PLC! s2\")) (set! estrutura (set-attr! estrutura atributo4 #t)) (define result (string-append (string-append (get-attr estrutura atributo1) (get-attr estrutura atributo2)) (get-attr estrutura atributo3))) result)"
(begin
(define estrutura
(create-struct '(atributo1 atributo2 atributo3 atributo4))
)
(set! estrutura (set-attr! estrutura atributo1 "Nós "))
(set! estrutura (set-attr! estrutura atributo2 "amamos "))
(set! estrutura (set-attr! estrutura atributo3 "PLC! s2"))
(set! estrutura (set-attr! estrutura atributo4 #t))
(define result (string-append (string-append (get-attr estrutura atributo1) (get-attr estrutura atributo2)) (get-attr estrutura atributo3)))
result
)
; PROGRAMA 9 - fatorial:
; String: "(begin (define fatorial (lambda (x) (if (eqv? x 0) 1 (* (fatorial (- x 1)) x)))) (define result (fatorial 5)) result)"
(begin
(define fatorial
(lambda (x)
(if (eqv? x 0) 1 (* (fatorial (- x 1)) x))
)
)
(define result (fatorial 5))
result
)
; PROGRAMA 10 - somatório de uma lista:
; String: "(begin (define sum (lambda (x) (if (null? x) 0 (+ (sum (cdr x)) (car x))))) (define result (sum '(1 2 3 4 5))) result)"
(begin
(define sum
(lambda (x)
(if (null? x) 0 (+ (sum (cdr x)) (car x)))
)
)
(define result (sum '(1 2 3 4 5)))
result
)