Skip to content
Snippets Groups Projects
Commit 8ea3c42d authored by Radu-Andrei Coanda's avatar Radu-Andrei Coanda
Browse files

ub11

parent 024eedaf
Branches
No related tags found
No related merge requests found
-- Constructor
data Mobile a = deriving Show
data Mobile a = Stern | Seepferdchen | Elefant (Mobile a) | Kaenguru a (Mobile a) (Mobile a) deriving Show
-- Examples
mobileLinks :: Mobile Int
......@@ -18,11 +18,22 @@ mobileRechts = Elefant ( Kaenguru 1 ( Elefant Stern ) ( Elefant Seepferdchen ))
-- Part b:
count :: Mobile a -> Int
count Stern = 1
count Seepferdchen = 1
count (Elefant m) = 1 + count m
count (Kaenguru _ m1 m2) = 1 + count m1 + count m2
-- Part c:
liste :: Mobile a -> [a]
liste Stern = []
liste Seepferdchen = []
liste (Elefant m) = liste m
liste (Kaenguru v m1 m2) = v : liste m1 ++ liste m2
-- Part d:
greife :: Mobile a -> Int -> Mobile a
greife e 1 = e
greife (Elefant m) n = greife m (n-1)
greife (Kaenguru _ m1 m2) n | (n-1) <= count m1 = greife m1 (n-1)
| otherwise = greife m2 (n - 1 - count m1)
-- Part f
f :: a -> b -> c -> d
f :: [a'] -> b -> c -> d
f :: [a'] -> b -> d -> d
f :: [a'] -> [a'] -> d -> d
f [] x y = y
f [ z : zs ] x y = f [] ( z : x ) y
-- Part g
g :: a -> b -> c
g :: a -> Int -> Int
g :: Int -> Int -> Int
g x 1 = 1
g x y = (\ x -> ( g x 0)) y
-- Part h
h :: a -> b -> c -> d
h :: [Bool] -> Bool -> [Bool] -> d
h ( x : xs ) y z = if x then h xs x ( y : z ) else h xs y z
-- Part i
data T a b = C0 | C1 a | C2 b | C3 ( T a b ) ( T a b )
i :: m -> n
i :: T m1 m2 -> T n1 n2
i :: T m1 m2 -> T n1 Int
i :: T [m1'] m2 -> T n1 Int
i :: T [m1'] m1' -> T n1 Int
i ( C3 ( C1 x ) ( C2 y )) = C2 0
i ( C3 ( C1 ( x : xs )) ( C2 y )) = i ( C3 ( C1 [ y ]) ( C2 x ))
......@@ -50,8 +50,8 @@ flattenTree'' t = foldTree flattenN [] t
-- Part a:
prodTree :: Tree -> Int
prodTree t =
prodTree t = foldTree (\ v l r -> v * l * r) 1 t
-- Part b:
incTree :: Tree -> Tree
incTree t =
\ No newline at end of file
incTree t = foldTree (\ v l r -> Node ( v + 1) l r) Nil t
\ No newline at end of file
-- Part a:
odds :: [Int]
odds = [2]
odds = 1 : map (+2) odds
-- Part b:
primeFactor :: Int -> [Int]
primeFactor =
primeFactor = primeH primes
where primeH (x:xs) y | y == 1 = []
| mod y x == 0 = x : primeH (x:xs) (div y x)
| otherwise = primeH xs y
-- Helper Functions:
from :: Int-> [Int]
......
......@@ -22,7 +22,13 @@ hatRang(mus, student).
% Teilaufgabe b )
% Teilaufgabe c )
bossVon(X, Y) :- hatRang(X, professor), hatRang(Y, assistent).
bossVon(X, Y) :- hatRang(X, assistent), hatRang(Y, tutor).
bossVon(X, Y) :- hatRang(X, tutor), hatRang(Y, student).
% Teilaufgabe d )
% Teilaufgabe e )
vorgesetzt(X,Y) :- bossVon(X, Y).
vorgesetzt(X,Y) :- bossVon(X, Z), vorgesetzt(Z, Y).
\ No newline at end of file
File added
File added
File added
File added
File added
% Part (a)
% Test Aufruf: increment(node(leaf(s(0)),s(s(0)),leaf(0)),Res).
increment(leaf(X), leaf(s(X))).
increment(node(L,V,R), node(IncL, s(V), IncR)) :- increment(L, IncL), increment(R, IncR).
% Part (b)
% Test Aufruf: append([a,b,c],[d,e],Res).
append([], Ys, Ys).
append([X|Xs], Ys, [X|Res]) :- append(Xs, Ys, Res).
% Part (c)
% Test Aufruf: inorder(node(leaf(s(0)),s(s(0)),node(leaf(s(0)),0,leaf(s(s(s(0)))))), Res).
inorder(leaf(X), [X]).
inorder(node(L,V,R), Res) :- inorder(L, ResL), inorder(R, ResR), append(ResL, [V|ResR], Res).
\ No newline at end of file
% prime(N) ist genau dann wahr, wenn N > 1 und N eine Primzahl ist.
prime(N) :- N > 1, X is N-1, nodivisors(N, X).
% nodivisors(N, X) ist genau dann wahr, wenn N keine Teiler zwischen X und 2 hat.
nodivisors(_, 1).
nodivisors(N, X) :- notdivisors(N, X), Y is X-1, nodivisors(N, Y).
% notdivisor (N , X ) ist wahr gdw . N nicht durch X teilbar ist .
notdivisors(N, X) :- Y is N mod X, Y > 0.
\ No newline at end of file
% Part (a)
% Test Aufruf: increment(node(leaf(s(0)),s(s(0)),leaf(0)),Res).
% Part (b)
% Test Aufruf: append([a,b,c],[d,e],Res).
% Part (c)
% Test Aufruf: inorder(node(leaf(s(0)),s(s(0)),node(leaf(s(0)),0,leaf(s(s(s(0)))))), Res).
\ No newline at end of file
% prime(N) ist genau dann wahr, wenn N > 1 und N eine Primzahl ist.
% nodivisors(N, X) ist genau dann wahr, wenn N keine Teiler zwischen X und 2 hat.
% notdivisor (N , X ) ist wahr gdw . N nicht durch X teilbar ist .
\ No newline at end of file
% Part (a)
% Test Aufruf: increment(node(leaf(s(0)),s(s(0)),leaf(0)),Res).
% Part (b)
% Test Aufruf: append([a,b,c],[d,e],Res).
% Part (c)
% Test Aufruf: inorder(node(leaf(s(0)),s(s(0)),node(leaf(s(0)),0,leaf(s(s(s(0)))))), Res).
\ No newline at end of file
% prime(N) ist genau dann wahr, wenn N > 1 und N eine Primzahl ist.
% nodivisors(N, X) ist genau dann wahr, wenn N keine Teiler zwischen X und 2 hat.
% notdivisor (N , X ) ist wahr gdw . N nicht durch X teilbar ist .
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment