Add basic checks for UE3 A.4, fix UE3 A.3

This commit is contained in:
Ivaylo Ivanov 2021-11-04 17:47:01 +01:00
parent 6d705ad01e
commit 32f7a83d2d
1 changed files with 61 additions and 9 deletions

View File

@ -29,7 +29,7 @@ Solution:
> matrixToString :: Matrix -> String -> String
> matrixToString matrix res
> | matrix == fehlerwert = "()"
> | isEmpty matrix = "()"
> | length m /= 0 && null ms = res ++ show m ++ ")"
> | otherwise = matrixToString restMatrix (res ++ show m ++ " ")
> where
@ -45,13 +45,14 @@ Solution:
> matrixtyp :: Matrix -> Matrixtyp
> matrixtyp m
> | m == fehlerwert = KeineMatrix
> | isEmpty m = KeineMatrix
> | isCorrectMatrix m 0 == False = KeineMatrix
> | otherwise = getMatrixType m
> isCorrectMatrix :: Matrix -> Int -> Bool
> isCorrectMatrix matrix n
> | matrix == fehlerwert = True
> | isEmpty matrix = True
> | n == 0 && length m == 0 = False
> | n == 0 && length m /= 0 = isCorrectMatrix matrix (length m)
> | n == length m = isCorrectMatrix (M ms) n
> | otherwise = False
@ -64,14 +65,65 @@ Solution:
Aufgabe A.3
> instance Eq Matrix where
> (==) (M m1) (M m2) = (m1 Prelude.== m2)
> (/=) (M m1) (M m2) = (m1 Prelude./= m2)
> (==) (M m1) (M m2)
> | m1 Prelude.== [] || m2 Prelude.== [] = error "Gleichheit undefiniert"
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = error "Gleichheit undefiniert"
> | otherwise = (m1 Prelude.== m2)
> (/=) (M m1) (M m2)
> | m1 Prelude.== [] || m2 Prelude.== [] = error "Ungleichheit undefiniert"
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = error "Ungleichheit undefiniert"
> | otherwise = (m1 Prelude./= m2)
> isEmpty :: Matrix -> Bool
> isEmpty (M m)
> | null m = True
> | otherwise = False
Aufgabe A.4
Knapp, aber gut nachvollziebar geht die Instanzdeklaration fuer Num folgendermassen vor:
...
> instance Num Matrix where
> (+) m1 m2 = (add m1 m2)
> (-) m1 m2 = (diff m1 m2)
> (*) m1 m2 = (mult m1 m2)
> negate m = negation m
> abs m = absolute m
> signum m = sign m
> fromInteger z = (M [[fromIntegral z]])
> add :: Matrix -> Matrix -> Matrix
> add (M m1) (M m2)
> | isEmpty (M m1) || isEmpty (M m2) = fehlerwert
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = fehlerwert
> | matrixtyp (M m1) /= matrixtyp (M m2) = fehlerwert
> | otherwise = M ([[0]])
instance Num Matrix where
...
> diff :: Matrix -> Matrix -> Matrix
> diff (M m1) (M m2)
> | isEmpty (M m1) || isEmpty (M m2) = fehlerwert
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = fehlerwert
> | matrixtyp (M m1) /= matrixtyp (M m2) = fehlerwert
> | otherwise = M ([[0]])
> mult :: Matrix -> Matrix -> Matrix
> mult (M m1) (M m2)
> | isEmpty (M m1) || isEmpty (M m2) = fehlerwert
> | matrixtyp (M m1) == KeineMatrix || matrixtyp (M m2) == KeineMatrix = fehlerwert
> | otherwise = M ([[0]])
> negation :: Matrix -> Matrix
> negation (M m1)
> | isEmpty (M m1)= fehlerwert
> | matrixtyp (M m1) == KeineMatrix = fehlerwert
> | otherwise = M ([[0]])
> absolute :: Matrix -> Matrix
> absolute (M m1)
> | isEmpty (M m1)= fehlerwert
> | matrixtyp (M m1) == KeineMatrix = fehlerwert
> | otherwise = M ([[0]])
> sign :: Matrix -> Matrix
> sign (M m1)
> | isEmpty (M m1)= fehlerwert
> | matrixtyp (M m1) == KeineMatrix = fehlerwert
> | otherwise = M ([[0]])