-
Notifications
You must be signed in to change notification settings - Fork 0
/
aufgabe12_16.hs
49 lines (39 loc) · 959 Bytes
/
aufgabe12_16.hs
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
-- Aufgabe 12.16
-- a)
-- Note that expo work only on natural numbers (non-negative)
-- `expo 0 0`, `expo 0 1`, `expo 2 3`
expo :: Int -> Int -> Int
expo x 0 = 1
expo 0 y = 0
expo x y
| y > 0 = x * (expo x (y-1))
{-
Computation example:
expo 2 2 = 2 * (expo 2 1)
= 2 * 2 * (expo 2 0)
= 2 * 2 * 1
= 4
-}
-- b)
-- `check (Branch (Branch (Leaf 1) (Leaf 2)) (Leaf 6)) k` mit 0<=k<=4
data Tree a = Branch (Tree a) (Tree a) | Leaf a deriving Show
check :: Tree a -> Int -> Bool
check (Leaf a) k
| k == 1 = True
| k <= 0 = False
| k > 1 = False
check (Branch t1 t2) k
| (check t1 (k-1)) || (check t2 (k-1)) = True
| otherwise = False
-- c)
-- `test [1,0,2,2]` or `test [1,0,2,3]`
test :: [Int] -> Bool
test [] = True
test (x:xs)
| (compare_test x xs) && (test xs) = True
| otherwise = False
compare_test :: Int -> [Int] -> Bool
compare_test x [] = True
compare_test x (y:ys)
| x /= y && (compare_test x ys) = True
| otherwise = False