Add (almost) correct function implemenations for all types in UE5

This commit is contained in:
Ivaylo Ivanov 2021-11-18 18:26:53 +01:00
parent c1208da5b9
commit d288b49fcf
2 changed files with 19 additions and 13 deletions

View File

@ -27,7 +27,7 @@ class Eq a => Menge_von a where
-- Protoimplementierungen
leer = []
vereinige xs ys = clear_duplicates (xs ++ ys)
vereinige xs ys = xs ++ ys
schneide xs ys = xs \\ (xs \\ ys)
ziehe_ab xs ys = xs \\ (intersectBy (==) xs ys)
ist_teilmenge xs ys = (length (ziehe_ab xs ys) == 0)
@ -37,14 +37,6 @@ class Eq a => Menge_von a where
ist_leer xs = xs == leer
sind_gleich xs ys = ist_teilmenge xs ys && ist_teilmenge ys xs
clear_duplicates :: Eq a => [a] -> [a]
clear_duplicates [] = []
clear_duplicates (x:xs) = x : filter (/= x) (clear_duplicates xs)
has_duplicates:: Eq a => [a] -> Bool
has_duplicates [] = False
has_duplicates (x:xs) = if x `elem` xs then True else has_duplicates xs
-- Weitere Typen:
newtype Paar a b = P (a,b) deriving (Eq,Show)
@ -101,6 +93,14 @@ is_correct_num n =
F -> False
_ -> True
clear_duplicates :: Eq a => [a] -> [a]
clear_duplicates [] = []
clear_duplicates (x:xs) = x : filter (/= x) (clear_duplicates xs)
has_duplicates:: Eq a => [a] -> Bool
has_duplicates [] = False
has_duplicates (x:xs) = if x `elem` xs then True else has_duplicates xs
-- Aufgabe A.1
instance Num Zahlraum_0_10 where
@ -163,10 +163,13 @@ build_pairs fkt arg
-- Aufgabe A.3
instance Menge_von Int where
vereinige xs ys = clear_duplicates (xs ++ ys)
instance Menge_von Zahlraum_0_10 where
vereinige xs ys = clear_duplicates (xs ++ ys)
instance Menge_von Funktion where
vereinige xs ys = clear_duplicates (xs ++ ys)
-- Aufgabe A.4
@ -180,14 +183,17 @@ instance Eq a => Eq (ElemTyp a) where
(==) (ET a) (ET b) = a == b
instance Show a => Show (ElemTyp a) where
show (ET a) = show a
-- Aufgabe A.6
instance Eq a => Menge_von (ElemTyp a) where
anzahl x xs = (length . filter (== x)) xs
-- Aufgabe A.7
instance (Eq a,Eq b,Eq c,Eq d,Eq e) => Menge_von (PH_ElemTyp a b c d e) where
vereinige xs ys = clear_duplicates (xs ++ ys)
-- Aufgabe A.8

View File

@ -78,10 +78,10 @@ spec =
funktion_tests,
menge_von_int_tests,
menge_von_zahlraum_0_10_tests,
menge_von_funktion_tests
-- menge_von_ElemTyp_tests,
-- ph_elemtyp_tests,
-- ph_elemtyp'_tests
menge_von_funktion_tests,
menge_von_ElemTyp_tests,
ph_elemtyp_tests,
ph_elemtyp'_tests
]
zahlraum_0_10_tests :: TestTree