-
Notifications
You must be signed in to change notification settings - Fork 0
/
Labb #1.scm
187 lines (115 loc) · 3.4 KB
/
Labb #1.scm
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
;Problem 1
(define foo
(* 1 2 3 4))
(define (foobar)
(* 1 2 3 4))
;foo - binder 24 till foo
;foobar - säger #<procedure:foobar>. Dvs talar om att foobar är en procedur
;(foo) - Försöker köra en procedur av namnet foo men får en konstant.
;(foobar) - 24. (foobar) är en procedur. När den körs fås värdet 24.
;Problem 2
;(define (sum n)
;(* n (/ (+ 1 n) 2)))
;(define (sum n)
; (if (= n 0)
; 0
; (+ n (sum (- n 1)))))
(define (sum n)
(define (sum-help n res)
(if(= n 1)
res
(sum-help (- n 1) (+ res n))))
(if (= n 0)
0
(sum-help n 1)))
;Problem 3
;(define (powers-of-two n)
; (if (= n 0)
; 1
; ( * 2 (powers-of-two (- n 1)))))
;Problem 4
;(define (pascal rad kol)
; (cond
; ((= 1 kol) 1)
; ((= rad kol) 1)
; (else (+ (pascal (- rad 1) kol) (pascal (- rad 1) (- kol 1))))))
;Problem 5
(define (my-* num1 num2)
(if (= num2 0)
0
(+ num1 (my-* num1 (- num2 1)))))
(define (double num)
(* num 2))
(define (halve num)
(/ num 2))
(define (multi num1 num2)
(cond
((= num2 0) 0)
((even? num2) (multi (* num1 2) (/ num2 2)))
(else (+ num1 (multi num1 (- num2 1))))))
;Problem 6
(define (last-digit n)
(remainder n 10))
(define (but-last-digit n)
(quotient n 10))
(define (sum-of-digits n)
(if (= n 0) 0
(+ (last-digit n) (sum-of-digits (but-last-digit n)))))
(define (number-of-digits n)
(if (= n 0) 0
(+ 1 (number-of-digits (but-last-digit n)))))
(define (divisible? t n)
(if (= (remainder t n) 0) #t #f))
(define (random-from-to from to)
(+ from (random (- (+ to 1) from))))
;Problem 7
(define (simple-sv-num? tal nämnare)
(divisible? (sum-of-digits tal) nämnare))
(define (make-simple-sv-num nämnare)
(let ((slump (random-from-to 100000 999999)))
(if (divisible? (sum-of-digits slump) nämnare)
slump
(make-simple-sv-num nämnare))))
;Problem 8
(define (make-cc-sv-sum)
(let ((slump (random-from-to 100000 999999)))
(define (helper slump)
(if (= slump 0)
0
(+ (cond
((odd? (number-of-digits slump)) (last-digit slump))
((>= (last-digit slump) 5) (+ (* (last-digit slump) 2) 1))
((< (last-digit slump) 5) (* (last-digit slump) 2)))
(helper (but-last-digit slump)))))
(if (divisible? (helper slump) 10)
slump
(make-cc-sv-sum))))
;Problem 9
(define (sum-and-apply-to-digits number digit-proc)
(if (= (number-of-digits number) 1)
(digit-proc number 1)
(+ (digit-proc (last-digit number) (number-of-digits number))
(sum-and-apply-to-digits (but-last-digit number) digit-proc))))
(define (number-of-digits2 number)
(sum-and-apply-to-digits number (lambda (digit pos) 1)))
(define (sum-of-digits2 number)
(sum-and-apply-to-digits number (lambda (digit pos) digit)))
(define (make-cc-sv-num2)
(let ((slump (random-from-to 100000 999999)))
(if (divisible? (sum-and-apply-to-digits slump prochelper) 10)
slump
(make-cc-sv-num2))))
(define (prochelper digit pos)
(cond
((odd? pos) digit)
((> 5 digit) (* 2 digit))
((+ (* 2 digit) 1))))
;Problem 10
;(define (personnummer-helper nummer position)
; (if (even? position)
; nummer
; (sum-of-digits (* nummer 2))))
;(define (personnummer nummer)
;(if (divisible? (sum-and-apply-to-digits nummer personnummer-helper) 10)
; #t
; #f))