Lösungen zum 4. Übungsblatt/Testatblatt Informatik I
Lösungen zum 4. Übungsblatt/Testatblatt Informatik I
Lösungen zum 4. Übungsblatt/Testatblatt Informatik I
Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.
YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.
Aufgabe 1<br />
<strong>Lösungen</strong> <strong>zum</strong> <strong>4.</strong> <strong>Übungsblatt</strong>/<strong>Testatblatt</strong><br />
<strong>Informatik</strong> I<br />
David Borowsky, Tobias Müller<br />
Gruppe Björn Appel<br />
Tübingen, den 23. November 2004<br />
1 ; (list 1 (cons 2 3) (list 4 5 6))<br />
2 (cons 1 (cons (cons 2 3) (cons (cons 4 (cons 5 (cons 6 ’()))) ’())))<br />
3 ; (list (cons 1 ’eins) (cons 2 ’zwei) (cons 3 ’drei))<br />
4 (cons (cons 1 ’eins) (cons (cons 2 ’zwei) (cons (cons 3 ’drei) ’())))<br />
5 ; (list 1 2 3)<br />
6 (cons 1 (cons 2 (cons 3 ’())))<br />
7 ; sollte (cons (list 1 2 3) (list ’eins ’zwei ’drei)) sein, wird aber<br />
8 ; (logischerweise) zu (list (list 1 2 3) ’eins ’zwei ’drei)<br />
9 (cons (cons 1 (cons 2 (cons 3 ’()))) (cons ’eins (cons ’zwei (cons ’drei ’()))))<br />
Aufgabe 2<br />
Die Funktionen sind genau dann primitiv-rekursiv, wenn sie sich darstellen lassen, als<br />
h(x1, . . . , xn, 0) = f(x1, . . . , xn)<br />
h(x1, . . . , xn, y + 1) = g(x1, . . . , xn, y, h(x1, . . . , xn, y))<br />
a) (n, m) ↦→ n + m sei die Funktion add(n, m). Dann können wir add(n, m) darstellen als:<br />
add(n, 0) = f(n)<br />
add(n, y + 1) = g(n, y, add(n, y))<br />
wobei<br />
f(n) = n<br />
g(n, y, a) = a + 1<br />
b) n ↦→ 3 n sei die Funktion dreihoch(n). Dann können wir dreihoch(n) darstellen als:<br />
dreihoch(0) = f()<br />
dreihoch(y + 1) = g(y, dreihoch(y))<br />
wobei<br />
f() = 1<br />
g(y, a) = 3 · a<br />
1
<strong>Lösungen</strong> <strong>zum</strong> <strong>4.</strong> <strong>Übungsblatt</strong>/<strong>Testatblatt</strong><br />
<strong>Informatik</strong> I 2<br />
c) n ↦→ n! sei die Funktion fakultaet(n). Dann können wir fakultaet(n) darstellen als:<br />
Aufgabe 3<br />
fakultaet(0) = f()<br />
fakulteat(y + 1) = g(y, fakultaet(y))<br />
wobei<br />
f() = 1<br />
g(y, a) = (y + 1) · a<br />
• (D, ⊑) ist flach.<br />
Dann ist ⊑ die Identität und damit ist di ⊑ di+1 = di∀i. Da di = di+1 bricht die Kette ab, da ∃i : di =<br />
di+k∀k.<br />
• D ist endlich<br />
Aufgabe 4<br />
1 ; *** CONSTS ************************************************************************************************<br />
2 (define pi 3.1415926535897932384626433832795)<br />
4 ; *** RECORDS ***********************************************************************************************<br />
6 ; Ein Shape ist entweder<br />
7 ; ein Circle<br />
8 ; ein Rectangle<br />
9 ; ein Triangle<br />
11 ; Ein Point ist ein Record (make-point x y)<br />
12 ; wobei<br />
13 ; x und y Zahlen sind<br />
14 (define-record-procedures point make-point point? (point-x point-y))<br />
16 ; Ein Circle ist ein Record (make-circle coords radius)<br />
17 ; wobei<br />
18 ; coords ein Punkt (point) ist (Mittelpunkt/Schwerpunkt)<br />
19 ; radius eine Zahl ist (Radius)<br />
20 (define-record-procedures circle make-circle circle? (circle-coords circle-radius))<br />
22 ; Ein Rectangle ist ein Record (make-rectangle coords width height)<br />
23 ; wobei<br />
24 ; coords ein Punkt (point) ist (Mittelpunkt/Schwerpunkt)<br />
25 ; width eine Zahl ist (Breite)<br />
26 ; height eine Zahl ist (Höhe)<br />
27 (define-record-procedures rectangle make-rectangle rectangle? (rectangle-coords rectangle-width rectangle-height))<br />
29 ; Ein Triangle ist ein Record (make-triangle coords)<br />
30 ; wobei<br />
31 ; coords ein Punkt (point) ist (Schwerpunkt)<br />
32 ; width eine Zahl ist (Grundfläche)<br />
33 ; height eine Zahl ist (Höhe)<br />
34 (define-record-procedures triangle make-triangle triangle? (triangle-coords triangle-width triangle-height))<br />
36 ; *** FUNCTIONS ******************************************************************************************<br />
38 ; Beschreibung: Berechnet den Flächeninhalt einer geometrischen Figur vom Typ shape aus<br />
39 ; Vertrag: area: shape -> number<br />
41 (define area<br />
42 (lambda (a-shape)<br />
43 (cond<br />
44 ((circle? a-shape) (circle-area a-shape))<br />
45 ((rectangle? a-shape) (rectangle-area a-shape))<br />
46 ((triangle? a-shape) (triangle-area a-shape))<br />
47 (else (display "Error: Unknown shape"))<br />
48 )))<br />
David Borowsky, Tobias Müller<br />
Gruppe Björn Appel
<strong>Lösungen</strong> <strong>zum</strong> <strong>4.</strong> <strong>Übungsblatt</strong>/<strong>Testatblatt</strong><br />
<strong>Informatik</strong> I 3<br />
50 ; Hilfsfunktionen<br />
51 ; Vertrag jeweils: TYP-area: TYP -> number , wobei bei Typ circle/rectangle/triangle<br />
53 (define circle-area<br />
54 (lambda (a-circle)<br />
55 (* pi (circle-radius a-circle) (circle-radius a-circle))<br />
56 ))<br />
58 (define rectangle-area<br />
59 (lambda (a-rectangle)<br />
60 (* (rectangle-width a-rectangle) (rectangle-height a-rectangle))<br />
61 ))<br />
63 (define triangle-area<br />
64 (lambda (a-triangle)<br />
65 ( * 172 (triangle-width a-triangle) (triangle-height a-triangle))<br />
66 ))<br />
68 ; Beschreibung: Gibt die X-Koordinate einer geometrischen Figur vom Typ shape aus<br />
69 ; Vertrag: x-coord: shape -> number<br />
71 (define x-coord<br />
72 (lambda (a-shape)<br />
73 (cond<br />
74 ((circle? a-shape) (circle-x-coord a-shape))<br />
75 ((rectangle? a-shape) (rectangle-x-coord a-shape))<br />
76 ((triangle? a-shape) (triangle-x-coord a-shape))<br />
77 (else (display "Error: Unknown shape"))<br />
78 )))<br />
80 ; Hilfsfunktionen<br />
81 ; Vertrag jeweils: TYP-x-coord: TYP -> number , wobei bei Typ circle/rectangle/triangle<br />
83 (define circle-x-coord<br />
84 (lambda (a-circle)<br />
85 (point-x (circle-coords a-circle))<br />
86 ))<br />
88 (define rectangle-x-coord<br />
89 (lambda (a-rectangle)<br />
90 (point-x (rectangle-coords a-rectangle))<br />
91 ))<br />
93 (define triangle-x-coord<br />
94 (lambda (a-triangle)<br />
95 (point-x (triangle-coords a-triangle))<br />
96 ))<br />
98 ; Beschreibung: Gibt die Y-Koordinate einer geometrischen Figur vom Typ shape aus<br />
99 ; Vertrag: y-coord: shape -> number<br />
101 (define y-coord<br />
102 (lambda (a-shape)<br />
103 (cond<br />
104 ((circle? a-shape) (circle-y-coord a-shape))<br />
105 ((rectangle? a-shape) (rectangle-y-coord a-shape))<br />
106 ((triangle? a-shape) (triangle-y-coord a-shape))<br />
107 (else (display "Error: Unknown shape"))<br />
108 )))<br />
110 ; Hilfsfunktionen<br />
111 ; Vertrag jeweils: TYP-y-coord: TYP -> number , wobei bei Typ circle/rectangle/triangle<br />
113 (define circle-y-coord<br />
114 (lambda (a-circle)<br />
115 (point-y (circle-coords a-circle))<br />
116 ))<br />
118 (define rectangle-y-coord<br />
119 (lambda (a-rectangle)<br />
120 (point-y (rectangle-coords a-rectangle))<br />
121 ))<br />
123 (define triangle-y-coord<br />
124 (lambda (a-triangle)<br />
125 (point-y (triangle-coords a-triangle))<br />
126 ))<br />
128 ; Beschreibung: Berechnet ob die erste der beiden geometrischen Figuren vom Typ shape<br />
129 ; größer, gleich oder kleiner als die zweite ist.<br />
130 ; Vertrag: compare-area shape shape -> symbol<br />
132 (define compare-area<br />
133 (lambda (a-shape1 a-shape2)<br />
134 (cond<br />
David Borowsky, Tobias Müller<br />
Gruppe Björn Appel
<strong>Lösungen</strong> <strong>zum</strong> <strong>4.</strong> <strong>Übungsblatt</strong>/<strong>Testatblatt</strong><br />
<strong>Informatik</strong> I 4<br />
135 ((= (area a-shape1) (area a-shape2)) ’equal)<br />
136 ((< (area a-shape1) (area a-shape2)) ’smaller)<br />
137 ((> (area a-shape1) (area a-shape2)) ’bigger)<br />
138 (else (display "Error: Internal Error"))<br />
139 )))<br />
141 ; *** TESTS **********************************************************************<br />
143 ; 3 Testshapes<br />
144 (define c-shape (make-circle (make-point 1 2) 3))<br />
145 (define r-shape (make-rectangle (make-point 4 5) 6 7))<br />
146 (define t-shape (make-triangle (make-point 8 9) 10 11))<br />
148 (display "Überprüfung der Funktion x-coord")<br />
149 (equal? (x-coord c-shape) 1)<br />
150 (equal? (x-coord r-shape) 4)<br />
151 (equal? (x-coord t-shape) 8)<br />
153 (display "Überprüfung der Funktion y-coord")<br />
154 (equal? (y-coord c-shape) 2)<br />
155 (equal? (y-coord r-shape) 5)<br />
156 (equal? (y-coord t-shape) 9)<br />
158 (display "Überprüfung der Funktion area")<br />
159 (equal? (area c-shape) #i28.274333882308138)<br />
160 (equal? (area r-shape) 42)<br />
161 ; (area t-shape)<br />
163 (display "Überprüfung der Funktion compare-area")<br />
164 (equal? (compare-area r-shape r-shape) ’equal)<br />
165 (equal? (compare-area c-shape r-shape) ’smaller)<br />
166 (equal? (compare-area r-shape c-shape) ’bigger)<br />
Aufgabe 5<br />
Wir beweisen, dass fib(n) = Φn−Ψ n<br />
√ , wobei Ψ =<br />
5<br />
1−√5 2 und Φ = 1+√5 2 , durch vollständige Induktion.<br />
• Induktionsanfang (für n = 0 und n = 1)<br />
√ 0 √ 0 1− 5 1+ 5<br />
2 − 2<br />
√<br />
5<br />
√ 1 √ 1 1− 5 1+ 5<br />
2 − 2<br />
√<br />
5<br />
= 1 − 1<br />
=<br />
• Induktionsvorraussetzung (∀n)<br />
√ n √ n 1− 5 1+ 5<br />
2 − 2<br />
√<br />
5<br />
√ n+1 √ n+1 1− 5<br />
1+ 5<br />
2 − 2<br />
√<br />
5<br />
David Borowsky, Tobias Müller<br />
Gruppe Björn Appel<br />
√ 5 = 0 = fib(0)<br />
2 √ 5<br />
2<br />
√ 5 = 1 = fib(1)<br />
= fib(n)<br />
= fib(n + 1)
<strong>Lösungen</strong> <strong>zum</strong> <strong>4.</strong> <strong>Übungsblatt</strong>/<strong>Testatblatt</strong><br />
<strong>Informatik</strong> I 5<br />
• Induktionsschluß<br />
fib(n) + fib(n + 1) =<br />
David Borowsky, Tobias Müller<br />
Gruppe Björn Appel<br />
=<br />
=<br />
=<br />
=<br />
√ n √ n √ n+1 √ n+1 1− 5 1+ 5 1− 5<br />
1+ 5<br />
2 − 2<br />
2 − 2<br />
√ +<br />
√<br />
5<br />
5<br />
√ n √ n √ n+1 √ n+1 1− 5 1+ 5 1− 5<br />
1+ 5<br />
2 − 2 + 2 − 2<br />
√<br />
5<br />
√ n <br />
1− 5<br />
2 1 + 1−√ √ n <br />
5 1+ 5<br />
2 − 2 1 + 1+√ <br />
5<br />
2<br />
√<br />
5<br />
√ n √ 2 √ n √ 2 1− 5 1− 5 1+ 5 1+ 5<br />
2<br />
2 − 2<br />
2<br />
√<br />
5<br />
√ n+2 √ n+2 1− 5<br />
1+ 5<br />
2 − 2<br />
√<br />
5<br />
= fib(n + 2)