tut 10 & tut 11
This commit is contained in:
parent
94a3fd659e
commit
2aaea268fe
5 changed files with 146 additions and 0 deletions
41
tut10/aufgabe5.hs
Normal file
41
tut10/aufgabe5.hs
Normal file
|
@ -0,0 +1,41 @@
|
|||
fib :: Int -> Int
|
||||
fib 0 = 0
|
||||
fib 1 = 1
|
||||
fib n = fib (n-2) + fib (n-1)
|
||||
|
||||
primeTest :: Int -> Int -> Bool
|
||||
primeTest _ 1 = True
|
||||
primeTest x y | (rem x y == 0) = False
|
||||
| otherwise = primeTest x (y-1)
|
||||
|
||||
prime :: Int -> Bool
|
||||
prime x = primeTest x (x-1)
|
||||
|
||||
powersOfTwo :: Int -> Int -> [Int]
|
||||
powersOfTwo a b | a > b = []
|
||||
| otherwise = (2^a) : (powersOfTwo (a+1) b)
|
||||
|
||||
contains :: Int -> [Int] -> Bool
|
||||
contains _ [] = False
|
||||
contains x (y:ys) | x == y = True
|
||||
| otherwise = contains x ys
|
||||
|
||||
intersection :: [Int] -> [Int] -> [Int]
|
||||
intersection [] _ = []
|
||||
intersection (x:xs) ys | contains x ys = x:(intersection xs ys)
|
||||
| otherwise = intersection xs ys
|
||||
|
||||
selectSmallest :: [Int] -> Int -> Int
|
||||
selectSmallest [] y = y
|
||||
selectSmallest (x:xs) y | x < y = selectSmallest xs x
|
||||
| otherwise = selectSmallest xs y
|
||||
|
||||
removeFirst :: [Int] -> Int -> Bool -> [Int]
|
||||
removeFirst [] _ _ = []
|
||||
removeFirst xs _ True = xs
|
||||
removeFirst (x:xs) y False | x == y = removeFirst xs y True
|
||||
| otherwise = x:(removeFirst xs y False)
|
||||
|
||||
selectKsmallest :: Int -> [Int] -> Int
|
||||
selectKsmallest 1 (x:xs) = selectSmallest xs x
|
||||
selectKsmallest k (x:xs) = selectKsmallest (k-1) (removeFirst (x:xs) (selectSmallest xs x) False)
|
0
tut11/.projectile
Normal file
0
tut11/.projectile
Normal file
26
tut11/aufgabe1.hs
Normal file
26
tut11/aufgabe1.hs
Normal file
|
@ -0,0 +1,26 @@
|
|||
data Mobile a = Stern | Seepferdchen | Elefant (Mobile a)
|
||||
| Kaenguru a (Mobile a) (Mobile a) deriving Show
|
||||
|
||||
mobileRechts = Elefant (Kaenguru 1 (Elefant (Stern)) (Elefant (Seepferdchen)))
|
||||
|
||||
mobileLinks = Kaenguru 1 (Elefant
|
||||
(Kaenguru 2 (Stern) (Kaenguru 3 (Seepferdchen) (Stern)))
|
||||
) (Seepferdchen)
|
||||
|
||||
count :: Mobile a -> Int
|
||||
count Stern = 1
|
||||
count Seepferdchen = 1
|
||||
count (Elefant x) = 1 + count x
|
||||
count (Kaenguru _ y z) = 1 + count y + count z
|
||||
|
||||
liste :: Mobile a -> [a]
|
||||
liste Stern = []
|
||||
liste Seepferdchen = []
|
||||
liste (Elefant x) = liste x
|
||||
liste (Kaenguru x y z) = x : liste y ++ liste z
|
||||
|
||||
greife :: Mobile a -> Int -> Mobile a
|
||||
greife x 1 = x
|
||||
greife (Elefant x) i = greife x (i-1)
|
||||
greife (Kaenguru _ x y) i | (i-1) <= count x = greife x (i-1)
|
||||
| otherwise = greife y (i-1-(count x))
|
50
tut11/aufgabe5.hs
Normal file
50
tut11/aufgabe5.hs
Normal file
|
@ -0,0 +1,50 @@
|
|||
data Tree = Nil | Node Int Tree Tree deriving Show
|
||||
|
||||
testTree = Node 2
|
||||
(Node 4 (Node 9 Nil Nil) (Node 3 Nil (Node 7 Nil Nil)))
|
||||
(Node 17 Nil Nil)
|
||||
|
||||
|
||||
|
||||
|
||||
decTree :: Tree -> Tree
|
||||
decTree Nil = Nil
|
||||
decTree (Node v l r) = Node (v-1) (decTree l) (decTree r)
|
||||
|
||||
sumTree :: Tree -> Int
|
||||
sumTree Nil = 0
|
||||
sumTree (Node v l r) = v + (sumTree l) + (sumTree r)
|
||||
|
||||
flattenTree :: Tree -> [Int]
|
||||
flattenTree Nil = []
|
||||
flattenTree (Node v l r) = v : (flattenTree l) ++ (flattenTree r)
|
||||
|
||||
|
||||
|
||||
decTree' Nil = Nil
|
||||
decTree' (Node v l r) = decN v (decTree' l) (decTree' r)
|
||||
decN = \v l r -> Node (v - 1) l r
|
||||
|
||||
sumTree' Nil = 0
|
||||
sumTree' (Node v l r) = sumN v (sumTree' l) (sumTree' r)
|
||||
sumN = \v l r -> v + l + r
|
||||
|
||||
flattenTree' Nil = []
|
||||
flattenTree' (Node v l r) = flattenN v (flattenTree' l) (flattenTree' r)
|
||||
flattenN = \v l r -> v : l ++ r
|
||||
|
||||
|
||||
|
||||
foldTree :: (Int -> a -> a -> a) -> a -> Tree -> a
|
||||
foldTree f c Nil = c
|
||||
foldTree f c (Node v l r) = f v (foldTree f c l) (foldTree f c r)
|
||||
|
||||
decTree'' t = foldTree decN Nil t
|
||||
sumTree'' t = foldTree sumN 0 t
|
||||
flattenTree'' t = foldTree flattenN [] t
|
||||
|
||||
|
||||
prodTree :: Tree -> Int
|
||||
prodTree t = foldTree (\v l r -> v * l * r) 1 t
|
||||
|
||||
incTree t = foldTree (\v l r -> Node (v+1) l r) Nil t
|
29
tut11/aufgabe7.hs
Normal file
29
tut11/aufgabe7.hs
Normal file
|
@ -0,0 +1,29 @@
|
|||
from :: Int -> [Int]
|
||||
from x = x : from (x+1)
|
||||
|
||||
-- take :: Int -> [a] -> [a]
|
||||
-- take 0 _ = []
|
||||
-- take n (x:xs) = x : take (n-1) xs
|
||||
|
||||
drop_mult :: Int -> [Int] -> [Int]
|
||||
drop_mult x xs = filter (\y -> mod y x /= 0) xs
|
||||
|
||||
dropall :: [Int] -> [Int]
|
||||
dropall (x:xs) = x : dropall (drop_mult x xs)
|
||||
|
||||
primes :: [Int]
|
||||
primes = dropall (from 2)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
odds :: [Int]
|
||||
odds = 1 : map (+2) odds
|
||||
|
||||
pHelper _ 1 = []
|
||||
pHelper (x:xs) y | rem y x == 0 = x : pHelper (x:xs) (div y x)
|
||||
| otherwise = pHelper xs y
|
||||
primeFactors :: Int -> [Int]
|
||||
primeFactors = pHelper primes
|
||||
|
Reference in a new issue