Programmierung I - Blatt 11
Aus Infostudium Wiki
Inhaltsverzeichnis |
Aufgabe 1
length [] = 0 length (_:xs) = 1 + length xs head (x:_) = x facs 0 = [1] facs (n+1) = (n+1) * head (facs n) : facs n isEmpty1 [] = True isEmpty1 (_:_) = False isEmpty2 xs = length xs == 0
Siehe Auswertung
a)
isEmpty1(facs 2) |- isEmpty1((1+1)+head(facs 1):facs 1) |- False
b)
isEmpty2 (facs 2) // Pattern-Matching |- length (facs 2) == 0 // Lazy |- length ((1+1)*head (facs 1) : facs 1) == 0 // Pattern-Matching auf length: |- 1+length (facs 1) == 0 // Pattern-Matching auf facs: |- 1+length ((0+1)*head (facs 0) : facs 0) == 0 // Pattern-Matching auf length: |- 1+(1+length (facs 0)) == 0 // Pattern-Matching auf facs: |- 1+(1+length [1]) == 0 // Pattern-Matching auf length: |- 1+(1+(1+length [])) == 0 // Pattern-Matching auf length: |- 1+(1+(1+0)) == 0 // Lazy |- 1+(1+1) == 0 // Lazy |- 1+2 == 0 // Lazy |- 3 == 0 |- False
Aufgabe 2
a)
Musterlösung:
data Map a b = EmptyMap | Store a b (Map a b) deriving Show
Unsere Lösung:
data Map a = EmptyMap | Store String Int (Map a) deriving Show
Wir haben nur einen Parameter benutzt. Da in der Aufgabenstellung auch der Typ der Inhalte angegeben war, haben wir direkt String und Int benutzt.
b)
Musterlösung:
deleteMapping :: Eq a => a -> Map a b -> Map a b
deleteMapping _ EmptyMap = EmptyMap
deleteMapping x (Store y z m) =
let res = deleteMapping x m
in if x == y then res else Store y z res
Unsere Lösung:
deleteMappings :: String -> Map a -> Map a deleteMappings s EmptyMap = EmptyMap deleteMappings s (Store x y xs) = if s == x then (deleteMappings s xs) else Store x y (deleteMappings s xs)
c)
insertMapping :: a -> b -> Map a b -> Map a b insertMapping = Store
d)
getValues :: Eq a => a -> Map a b -> [b]
getValues _ EmptyMap = []
getValues x (Store y z m) =
(if x == y then (z:) else (\x -> x))
(getValues x m)
-- oder auch alternativ mit let wie in deleteMapping
-- = let res = getValues x m in if x == y then z:res else res
