This repository has been archived on 2021-11-27. You can view files and clone it, but cannot push or open issues or pull requests.
fprog2021WS/code/Angabe5.hs

195 lines
4.9 KiB
Haskell
Raw Normal View History

2021-11-15 17:09:27 +00:00
module Angabe5 where
2021-11-18 17:09:23 +00:00
import Data.List
2021-11-15 17:09:27 +00:00
{- 1. Vervollstaendigen Sie gemaess Angabentext!
2. Vervollständigen Sie auch die vorgegebenen Kommentaranfänge!
3. Loeschen Sie keine Deklarationen aus diesem Rahmenprogramm, auch nicht die Modulanweisug!
4. Achten Sie darauf, dass `Gruppe' Leserechte fuer Ihre Abgabedatei hat!
-}
type Nat0 = Int
-- Die selbstdefinierte Typklasse Menge_von:
class Eq a => Menge_von a where
2021-11-15 17:35:44 +00:00
leer :: [] a
vereinige :: [] a -> [] a -> [] a
schneide :: [] a -> [] a -> [] a
ziehe_ab :: [] a -> [] a -> [] a
ist_teilmenge :: [] a -> [] a -> Bool
ist_obermenge :: [] a -> [] a -> Bool
ist_element :: a -> [] a -> Bool
ist_leer :: [] a -> Bool
sind_gleich :: [] a -> [] a -> Bool
anzahl :: a -> [] a -> Nat0
-- Protoimplementierungen
leer = []
2021-11-18 17:09:23 +00:00
vereinige xs ys = clear_duplicates (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)
2021-11-15 17:35:44 +00:00
ist_obermenge xs ys = ist_teilmenge ys xs
ist_element x xs = anzahl x xs >= 1
2021-11-18 17:09:23 +00:00
anzahl x xs = if (has_duplicates xs) then error "Fehler" else (length . filter (== x)) xs
2021-11-15 17:35:44 +00:00
ist_leer xs = xs == leer
sind_gleich xs ys = ist_teilmenge xs ys && ist_teilmenge ys xs
2021-11-15 17:09:27 +00:00
2021-11-18 17:09:23 +00:00
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
2021-11-15 17:09:27 +00:00
-- Weitere Typen:
newtype Paar a b = P (a,b) deriving (Eq,Show)
data Zahlraum_0_10 = N | I | II | III | IV | V | VI
| VII | VIII | IX | X | F deriving (Eq,Ord,Show)
newtype Funktion = Fkt { f :: Zahlraum_0_10 -> Zahlraum_0_10 }
data Baum a = Blatt a | Knoten (Baum a) a (Baum a) deriving (Eq,Show)
newtype ElemTyp a = ET a
-- Pseudoheterogene Elementtypen
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)
2021-11-15 17:35:44 +00:00
-- 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
2021-11-15 17:09:27 +00:00
-- Aufgabe A.1
instance Num Zahlraum_0_10 where
2021-11-15 17:35:44 +00:00
(+) n n' = add_r n n'
(-) n n' = diff_r n n'
(*) n n' = mult_r n n'
fromInteger n = nat_to_roman (fromInteger n)
2021-11-18 16:05:21 +00:00
abs n = n
signum n
| n == F = F
| n == N = N
| otherwise = I
2021-11-15 17:35:44 +00:00
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
2021-11-15 17:09:27 +00:00
-- Aufgabe A.2
2021-11-18 16:05:21 +00:00
instance Eq Funktion where
(==) f f' = (show f) == (show f')
2021-11-15 17:09:27 +00:00
2021-11-18 16:05:21 +00:00
instance Show Funktion where
show f =
let
result = drop 1 (show (build_pairs f N))
in
"{" ++ take ((length result) -1) result ++ "}"
2021-11-15 17:09:27 +00:00
2021-11-18 16:05:21 +00:00
build_pairs :: Funktion -> Zahlraum_0_10 -> [(Zahlraum_0_10, Zahlraum_0_10)]
build_pairs fkt arg
| arg == F && z == F = [(F, F)]
| otherwise = (arg, z) : (build_pairs fkt (arg + I))
where
z = (f fkt) arg
2021-11-15 17:09:27 +00:00
-- Aufgabe A.3
2021-11-18 17:09:23 +00:00
instance Menge_von Int where
2021-11-15 17:09:27 +00:00
2021-11-18 17:09:23 +00:00
instance Menge_von Zahlraum_0_10 where
2021-11-15 17:09:27 +00:00
2021-11-18 17:09:23 +00:00
instance Menge_von Funktion where
2021-11-15 17:09:27 +00:00
-- Aufgabe A.4
2021-11-18 17:09:23 +00:00
instance (Eq a,Eq b) => Menge_von (Paar a b) where
2021-11-15 17:09:27 +00:00
2021-11-18 17:09:23 +00:00
instance Eq a => Menge_von (Baum a) where
2021-11-15 17:09:27 +00:00
-- Aufgabe A.5
2021-11-18 17:09:23 +00:00
instance Eq a => Eq (ElemTyp a) where
(==) (ET a) (ET b) = a == b
2021-11-15 17:09:27 +00:00
2021-11-18 17:09:23 +00:00
instance Show a => Show (ElemTyp a) where
2021-11-15 17:09:27 +00:00
-- Aufgabe A.6
2021-11-18 17:09:23 +00:00
instance Eq a => Menge_von (ElemTyp a) where
2021-11-15 17:09:27 +00:00
-- Aufgabe A.7
2021-11-18 17:09:23 +00:00
instance (Eq a,Eq b,Eq c,Eq d,Eq e) => Menge_von (PH_ElemTyp a b c d e) where
2021-11-15 17:09:27 +00:00
-- Aufgabe A.8
2021-11-18 17:09:23 +00:00
instance (Eq p,Eq q,Eq r) => Menge_von (PH_ElemTyp' p q r) where