0
|
1 module Exercise_1 where |
|
2 import Test.QuickCheck |
|
3 |
|
4 {---------------------------------------------------------------------} |
|
5 {- G1.1 -} |
|
6 threeDifferent :: Integer -> Integer -> Integer -> Bool |
1
|
7 threeDifferent x y z = (x /= y) && (y /= z) && (z /= x) |
|
8 |
0
|
9 |
|
10 fourEqual :: Integer -> Integer -> Integer -> Integer -> Bool |
1
|
11 fourEqual w x y z = (w == x) && (x == y) && (y == z) |
|
12 |
|
13 |
|
14 {- |
|
15 threeDifferent (2+3) 5 (11 ` div ` 2) |
|
16 ((2+3) /= 5) && (5 /= (11 ` div ` 2)) && ((11 ` div ` 2) /= (2+3)) |
|
17 (5 /= 5) && ((5 /= (11 ` div ` 2)) && ((11 ` div ` 2) /= (2+3))) |
|
18 False && ((5 /= (11 ` div ` 2)) && ((11 ` div ` 2) /= (2+3))) |
|
19 False |
|
20 |
|
21 Schritte von oben nach unten: |
|
22 threeDifferent ersetzen |
|
23 Linke Seite des ersten && |
|
24 5 /= 5 auswerten |
|
25 False && x = False (&& ist rechtsassoizativ) |
|
26 |
|
27 |
|
28 --- |
|
29 |
|
30 |
|
31 fourEqual (2+3) 5 (11 ` div ` 2) (21 ` mod ` 11) |
|
32 ((2+3) == 5) && (5 == (11 ` div ` 2)) && ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
33 (5 == 5) && (5 == (11 ` div ` 2)) && ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
34 True && (5 == (11 ` div ` 2)) && ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
35 (5 == (11 ` div ` 2)) && ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
36 (5 == 5) && ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
37 True && ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
38 ((11 ` div ` 2) == (21 ` mod ` 11)) |
|
39 (5 == (21 ` mod ` 11)) |
|
40 (5 == 10) |
|
41 False |
|
42 |
|
43 Schritte von oben nach unten: |
|
44 fourEqual ersetzen |
|
45 Linke Seite des ersten && |
|
46 5 == 5 auswerten |
|
47 True && x = x |
|
48 Linke Seite des zweiten && (div ist Ganzzahldivision) |
|
49 5 == 5 auswerten |
|
50 True && x = x |
|
51 Links nach rechts |
|
52 5 == 10 auswerten |
|
53 -} |
0
|
54 |
|
55 |
|
56 |
|
57 {---------------------------------------------------------------------} |
|
58 {- G1.2 -} |
|
59 fac :: Integer -> Integer |
1
|
60 fac 0 = 1 |
|
61 fac n | n > 0 = n * fac (n-1) |
|
62 |
0
|
63 |
|
64 sum_ten :: Integer -> Integer |
1
|
65 sum_ten n = sum_ten' n 9 |
|
66 where |
|
67 sum_ten' :: Integer -> Integer -> Integer |
|
68 sum_ten' n 0 = n |
|
69 sum_ten' n x = n + x + sum_ten' n (x-1) |
|
70 |
|
71 |
|
72 sum_ten_gauss :: Integer -> Integer |
|
73 sum_ten_gauss n = 10 * n + 45 |
0
|
74 |
|
75 |
|
76 |
|
77 {---------------------------------------------------------------------} |
|
78 {- G1.3 -} |
1
|
79 {- |
|
80 g ist leicht veraendert, um Mehrdeutigkeiten zu vermeiden. |
|
81 Original: |
|
82 g :: Integer -> Integer |
|
83 g n = if n < 10 then n*n else n |
|
84 -} |
|
85 |
0
|
86 g :: Integer -> Integer |
1
|
87 g n = if n < 10 then n*n else 2*n |
|
88 |
0
|
89 |
|
90 max_g :: Integer -> Integer |
1
|
91 max_g n |
|
92 | n <= 0 = 0 |
|
93 | otherwise = |
|
94 if (g x < g n) then n else x |
|
95 where |
|
96 x = max_g (n-1) |
|
97 |
0
|
98 |
1
|
99 max_g' :: Integer -> Integer |
|
100 max_g' n |
|
101 | n < 0 = 0 |
|
102 | n <= 9 = n |
|
103 -- Siehe Aenderung von g |
|
104 | n < 41 = 9 |
|
105 | otherwise = n |
|
106 |
|
107 |
|
108 prop_max_g :: Integer -> Bool |
|
109 prop_max_g n = |
|
110 max_g n == max_g' n |
|
111 |
|
112 |
|
113 |
|
114 {---------------------------------------------------------------------} |
0
|
115 {- Aufgabe H1.1 -} |
|
116 {-WETT-} {- Diese Zeile nicht entfernen -} |
|
117 sum_max_sq :: Integer -> Integer -> Integer -> Integer |
4
|
118 sum_max_sq x y z = max x y ^ 2 + min x y `max` z ^ 2 |
0
|
119 {-TTEW-} {- Diese Zeile nicht enfernen! -} |
|
120 |
|
121 |
|
122 |
|
123 {---------------------------------------------------------------------} |
|
124 {- Aufgabe H1.2 -} |
|
125 {- Teil 1 -} |
|
126 f :: Integer -> Integer |
4
|
127 f n |
|
128 | n > 100 = n - 10 |
|
129 | otherwise = f (f (n + 11)) |
|
130 |
0
|
131 |
|
132 {- Teil 2 -} |
|
133 {- |
4
|
134 - [(x, f x) | x <- [0,1,2,3,42,101,1001,-10]] |
|
135 - |
|
136 - ( x, f x) |
|
137 - ----------- |
|
138 - ( 0, 91) |
|
139 - ( 1, 91) |
|
140 - ( 2, 91) |
|
141 - ( 3, 91) |
|
142 - ( 42, 91) |
|
143 - ( 101, 91) |
|
144 - (1001, 991) |
|
145 - ( -10, 91) |
0
|
146 -} |
|
147 |
4
|
148 |
0
|
149 {- Teil 3 -} |
|
150 f' :: Integer -> Integer |
4
|
151 f' n = if n > 101 then n - 10 else 91 |
|
152 |
0
|
153 |
4
|
154 {- Quickcheck test -} |
|
155 prop_f n = f n == f' n |
0
|
156 |
|
157 |
|
158 |
|
159 {---------------------------------------------------------------------} |
|
160 {- Aufgabe H1.3 -} |
|
161 {- Teil 1 -} |
|
162 is_square' :: Integer -> Integer -> Bool |
4
|
163 is_square' n 0 = n == 0 |
|
164 is_square' n m |
|
165 | m < 0 = False |
|
166 | otherwise = m * m == n || is_square' n (m-1) |
|
167 |
0
|
168 |
|
169 {- Teil 2 -} |
|
170 is_square :: Integer -> Bool |
4
|
171 is_square n |
|
172 | n < 0 = False |
|
173 | otherwise = is_square' n n |