Add UE5 A.1
This commit is contained in:
parent
6b7ca28fe0
commit
711a2a1104
201
code/Angabe5.hs
201
code/Angabe5.hs
@ -13,25 +13,25 @@ type Nat0 = Int
|
|||||||
-- Die selbstdefinierte Typklasse Menge_von:
|
-- Die selbstdefinierte Typklasse Menge_von:
|
||||||
|
|
||||||
class Eq a => Menge_von a where
|
class Eq a => Menge_von a where
|
||||||
leer :: [] a
|
leer :: [] a
|
||||||
vereinige :: [] a -> [] a -> [] a
|
vereinige :: [] a -> [] a -> [] a
|
||||||
schneide :: [] a -> [] a -> [] a
|
schneide :: [] a -> [] a -> [] a
|
||||||
ziehe_ab :: [] a -> [] a -> [] a
|
ziehe_ab :: [] a -> [] a -> [] a
|
||||||
ist_teilmenge :: [] a -> [] a -> Bool
|
ist_teilmenge :: [] a -> [] a -> Bool
|
||||||
ist_obermenge :: [] a -> [] a -> Bool
|
ist_obermenge :: [] a -> [] a -> Bool
|
||||||
ist_element :: a -> [] a -> Bool
|
ist_element :: a -> [] a -> Bool
|
||||||
ist_leer :: [] a -> Bool
|
ist_leer :: [] a -> Bool
|
||||||
sind_gleich :: [] a -> [] a -> Bool
|
sind_gleich :: [] a -> [] a -> Bool
|
||||||
anzahl :: a -> [] a -> Nat0
|
anzahl :: a -> [] a -> Nat0
|
||||||
|
|
||||||
-- Protoimplementierungen
|
-- Protoimplementierungen
|
||||||
leer = []
|
leer = []
|
||||||
vereinige xs ys = xs ++ ys
|
vereinige xs ys = xs ++ ys
|
||||||
ist_teilmenge xs ys = ist_obermenge ys xs
|
ist_teilmenge xs ys = ist_obermenge ys xs
|
||||||
ist_obermenge xs ys = ist_teilmenge ys xs
|
ist_obermenge xs ys = ist_teilmenge ys xs
|
||||||
ist_element x xs = anzahl x xs >= 1
|
ist_element x xs = anzahl x xs >= 1
|
||||||
ist_leer xs = xs == leer
|
ist_leer xs = xs == leer
|
||||||
sind_gleich xs ys = ist_teilmenge xs ys && ist_teilmenge ys xs
|
sind_gleich xs ys = ist_teilmenge xs ys && ist_teilmenge ys xs
|
||||||
|
|
||||||
|
|
||||||
-- Weitere Typen:
|
-- Weitere Typen:
|
||||||
@ -52,121 +52,132 @@ data PH_ElemTyp a b c d e = A a | B b | C c | D d | E e deriving (Eq,Show)
|
|||||||
data PH_ElemTyp' q r s = Q q | R r | S s deriving (Eq,Show)
|
data PH_ElemTyp' q r s = Q q | R r | S s deriving (Eq,Show)
|
||||||
|
|
||||||
|
|
||||||
|
-- Simple helper functions
|
||||||
|
roman_to_nat :: Zahlraum_0_10 -> Nat0
|
||||||
|
roman_to_nat N = 0
|
||||||
|
roman_to_nat I = 1
|
||||||
|
roman_to_nat II = 2
|
||||||
|
roman_to_nat III = 3
|
||||||
|
roman_to_nat IV = 4
|
||||||
|
roman_to_nat V = 5
|
||||||
|
roman_to_nat VI = 6
|
||||||
|
roman_to_nat VII = 7
|
||||||
|
roman_to_nat VIII = 8
|
||||||
|
roman_to_nat IX = 9
|
||||||
|
roman_to_nat X = 10
|
||||||
|
roman_to_nat F = -1
|
||||||
|
|
||||||
|
nat_to_roman :: Nat0 -> Zahlraum_0_10
|
||||||
|
nat_to_roman 0 = N
|
||||||
|
nat_to_roman 1 = I
|
||||||
|
nat_to_roman 2 = II
|
||||||
|
nat_to_roman 3 = III
|
||||||
|
nat_to_roman 4 = IV
|
||||||
|
nat_to_roman 5 = V
|
||||||
|
nat_to_roman 6 = VI
|
||||||
|
nat_to_roman 7 = VII
|
||||||
|
nat_to_roman 8 = VIII
|
||||||
|
nat_to_roman 9 = IX
|
||||||
|
nat_to_roman 10 = X
|
||||||
|
nat_to_roman n
|
||||||
|
| n > 10 = F
|
||||||
|
| n < 0 = F
|
||||||
|
| otherwise = error "Could not recognize input" -- this should not happen
|
||||||
|
|
||||||
|
is_correct_num :: Zahlraum_0_10 -> Bool
|
||||||
|
is_correct_num n =
|
||||||
|
case n of
|
||||||
|
F -> False
|
||||||
|
_ -> True
|
||||||
|
|
||||||
-- Aufgabe A.1
|
-- Aufgabe A.1
|
||||||
|
|
||||||
instance Num Zahlraum_0_10 where
|
instance Num Zahlraum_0_10 where
|
||||||
...
|
(+) n n' = add_r n n'
|
||||||
|
(-) n n' = diff_r n n'
|
||||||
{- Knapp, aber gut nachvollziehbar geht die Instanzbildung fuer Num folgendermassen vor:
|
(*) n n' = mult_r n n'
|
||||||
...
|
fromInteger n = nat_to_roman (fromInteger n)
|
||||||
-}
|
|
||||||
|
|
||||||
|
add_r :: Zahlraum_0_10 -> Zahlraum_0_10 -> Zahlraum_0_10
|
||||||
|
add_r n n'
|
||||||
|
| is_correct_num n == False || is_correct_num n' == False = F
|
||||||
|
| otherwise = nat_to_roman sum_r
|
||||||
|
where
|
||||||
|
a = roman_to_nat n
|
||||||
|
b = roman_to_nat n'
|
||||||
|
sum_r = a + b
|
||||||
|
|
||||||
|
diff_r :: Zahlraum_0_10 -> Zahlraum_0_10 -> Zahlraum_0_10
|
||||||
|
diff_r n n'
|
||||||
|
| is_correct_num n == False || is_correct_num n' == False = F
|
||||||
|
| otherwise = nat_to_roman dif_r
|
||||||
|
where
|
||||||
|
a = roman_to_nat n
|
||||||
|
b = roman_to_nat n'
|
||||||
|
dif_r = a - b
|
||||||
|
|
||||||
|
mult_r :: Zahlraum_0_10 -> Zahlraum_0_10 -> Zahlraum_0_10
|
||||||
|
mult_r n n'
|
||||||
|
| is_correct_num n == False || is_correct_num n' == False = F
|
||||||
|
| otherwise = nat_to_roman prod_r
|
||||||
|
where
|
||||||
|
a = roman_to_nat n
|
||||||
|
b = roman_to_nat n'
|
||||||
|
prod_r = a * b
|
||||||
|
|
||||||
-- Aufgabe A.2
|
-- Aufgabe A.2
|
||||||
|
|
||||||
instance Eq Funktion where
|
-- instance Eq Funktion where
|
||||||
...
|
-- ...
|
||||||
|
|
||||||
instance Show Funktion where
|
|
||||||
...
|
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar gehen die beiden Instanzbildungen fuer
|
|
||||||
Eq und Show folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
|
||||||
|
-- instance Show Funktion where
|
||||||
|
-- ...
|
||||||
|
|
||||||
|
|
||||||
-- Aufgabe A.3
|
-- Aufgabe A.3
|
||||||
|
|
||||||
instance Menge_von Int where
|
-- instance Menge_von Int where
|
||||||
...
|
-- ...
|
||||||
|
|
||||||
instance Menge_von Zahlraum_0_10 where
|
|
||||||
...
|
|
||||||
|
|
||||||
instance Menge_von Funktion where
|
|
||||||
...
|
|
||||||
|
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar gehen die drei Instanzbildungen fuer
|
|
||||||
Menge_von folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
-- instance Menge_von Zahlraum_0_10 where
|
||||||
|
-- ...
|
||||||
|
|
||||||
|
-- instance Menge_von Funktion where
|
||||||
|
-- ...
|
||||||
|
|
||||||
|
|
||||||
-- Aufgabe A.4
|
-- Aufgabe A.4
|
||||||
|
|
||||||
instance (Eq a,Eq b) => Menge_von (Paar a b) where
|
-- instance (Eq a,Eq b) => Menge_von (Paar a b) where
|
||||||
...
|
-- ...
|
||||||
|
|
||||||
instance Eq a => Menge_von (Baum a) where
|
|
||||||
...
|
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar gehen die beiden Instanzbildungen fuer
|
|
||||||
Menge_von folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
-- instance Eq a => Menge_von (Baum a) where
|
||||||
|
-- ...
|
||||||
|
|
||||||
|
|
||||||
-- Aufgabe A.5
|
-- Aufgabe A.5
|
||||||
|
|
||||||
instance Eq a => Eq (ElemTyp a) where
|
-- instance Eq a => Eq (ElemTyp a) where
|
||||||
...
|
-- ...
|
||||||
|
|
||||||
instance Show a => Show (ElemTyp a) where
|
|
||||||
...
|
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar gehen die beiden Instanzbildungen fuer
|
|
||||||
Eq und Show folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
|
||||||
|
-- instance Show a => Show (ElemTyp a) where
|
||||||
|
-- ...
|
||||||
|
|
||||||
|
|
||||||
-- Aufgabe A.6
|
-- Aufgabe A.6
|
||||||
|
|
||||||
instance Eq a => Menge_von (ElemTyp a) where
|
-- instance Eq a => Menge_von (ElemTyp a) where
|
||||||
...
|
-- ...
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar geht die Instanzbildung fuer
|
|
||||||
Menge_von folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Aufgabe A.7
|
-- Aufgabe A.7
|
||||||
|
|
||||||
instance (Eq a,Eq b,Eq c,Eq d,Eq e) => Menge_von (PH_ElemTyp a b c d e) where
|
-- instance (Eq a,Eq b,Eq c,Eq d,Eq e) => Menge_von (PH_ElemTyp a b c d e) where
|
||||||
...
|
-- ...
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar geht die Instanzbildung fuer
|
|
||||||
Menge_von folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
-- Aufgabe A.8
|
-- Aufgabe A.8
|
||||||
|
|
||||||
instance (Eq p,Eq q,Eq r) => Menge_von (PH_ElemTyp' p q r) where ...
|
-- instance (Eq p,Eq q,Eq r) => Menge_von (PH_ElemTyp' p q r) where ...
|
||||||
|
|
||||||
|
|
||||||
{- Knapp, aber gut nachvollziehbar geht die Instanzbildung fuer
|
|
||||||
Menge_von folgendermassen vor:
|
|
||||||
...
|
|
||||||
-}
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user