Programmieren in Haskell Programmieren mit Listen Programmieren in Haskell 1
Was wir heute machen Eigener Listen-Datentyp Eine Sortierfunktion Nützliche Listenfunktionen Programmieren in Haskell 2
Ein eigener Listen-Datentyp data List a = Nil Cons a (List a) deriving (Show,Eq) Listen sind dann zum Beispiel: Nil :: List a Cons 1 (Cons 2 (Cons 3 Nil)) :: Num a => List a Cons "hallo" (Cons "welt" Nil) :: List [Char] Programmieren in Haskell 3
Spezifikation des Sortierproblems Gewünscht ist eine Funktion sortlist :: Ord a => List a -> List a, so daß für jede Liste xs :: List a (mit Ord a), die Elemente von sortlist xs geordnet sind, keine Elemente hinzukommen und keine verlorengehen. Programmieren in Haskell 4
Sortieren durch Einfügen isortlist :: Ord a => List a -> List a isortlist Nil = Nil isortlist (Cons x xs) = insertlist x (isortlist xs) where insertlist x Nil = Cons x Nil insertlist x (Cons y ys) x <= y = Cons x (Cons y ys) x > y = Cons y (insertlist x ys) Programmieren in Haskell 5
Sortieren durch Einfügen isortlist :: Ord a => List a -> List a isortlist Nil = Nil isortlist (Cons x xs) = insertlist x (isortlist xs) where insertlist x Nil = Cons x Nil insertlist x (Cons y ys) x <= y = Cons x (Cons y ys) x > y = Cons y (insertlist x ys) Beispiel: sortlist (Cons 1 (Cons 4 (Cons 2 Nil))) => Cons 1 (Cons 2 (Cons 4 Nil)) Programmieren in Haskell 5
Kopf und Restliste headlist :: List a -> a headlist Nil = error "headlist not defined for empty lists" headlist (Cons x xs) = x taillist :: List a -> List a taillist Nil = error "taillist not defined for empty lists" taillist (Cons x xs) = xs Programmieren in Haskell 6
Länge, Summe und Produkt lengthlist :: Num a => List b -> a lengthlist Nil = 0 lengthlist (Cons x xs) = 1 + lengthlist xs Programmieren in Haskell 7
Länge, Summe und Produkt lengthlist :: Num a => List b -> a lengthlist Nil = 0 lengthlist (Cons x xs) = 1 + lengthlist xs sumlist :: Num a => List a -> a sumlist Nil = 0 sumlist (Cons x xs) = x + sumlist xs Programmieren in Haskell 7
Länge, Summe und Produkt lengthlist :: Num a => List b -> a lengthlist Nil = 0 lengthlist (Cons x xs) = 1 + lengthlist xs sumlist :: Num a => List a -> a sumlist Nil = 0 sumlist (Cons x xs) = x + sumlist xs productlist :: Num a => List a -> a productlist Nil = 1 productlist (Cons x xs) = x * productlist xs Programmieren in Haskell 7
Aufzählungen enumfromtolist :: (Enum a, Ord a) => a -> a -> List a enumfromtolist a b a > b = Nil a == b = Cons a Nil a < b = Cons a (enumfromtolist (succ a) b) Programmieren in Haskell 8
Aufzählungen enumfromtolist :: (Enum a, Ord a) => a -> a -> List a enumfromtolist a b a > b = Nil a == b = Cons a Nil a < b = Cons a (enumfromtolist (succ a) b) Beispiele: enumfromtolist 1 5 => Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil)))) enumfromtolist 5 4 => Nil enumfromtolist a d => Cons a (Cons b (Cons c (Cons d Nil))) Programmieren in Haskell 8
Fakultät factoriallist :: Integral a => a -> a factoriallist n n < 0 = error "factoriallist not defined for negative values" otherwise = productlist (enumfromtolist 1 n) Programmieren in Haskell 9
Fakultät factoriallist :: Integral a => a -> a factoriallist n n < 0 = error "factoriallist not defined for negative values" otherwise = productlist (enumfromtolist 1 n) Beispiele: factoriallist 4 => 24, factoriallist 0 => 1 Programmieren in Haskell 9
Fakultät factoriallist :: Integral a => a -> a factoriallist n n < 0 = error "factoriallist not defined for negative values" otherwise = productlist (enumfromtolist 1 n) Beispiele: factoriallist 4 => 24, factoriallist 0 => 1 Alternative Definition ohne Listen: factorial :: Integral a => a -> a factorial n n < 0 = error "factorial not defined for negative values" n == 0 = 1 n > 0 = n * factorial (n-1) Programmieren in Haskell 9
Append appendlist :: List a -> List a -> List a appendlist Nil ys = ys appendlist (Cons x xs) ys = Cons x (appendlist xs ys) Programmieren in Haskell 10
Append appendlist :: List a -> List a -> List a appendlist Nil ys = ys appendlist (Cons x xs) ys = Cons x (appendlist xs ys) Beispiel: appendlist (enumfromtolist 1 5) (enumfromtolist 6 10) == enumfromtolist 1 10 => True Programmieren in Haskell 10
Reverse reverselistslow :: List a -> List a reverselistslow Nil = Nil reverselistslow (Cons x xs) = appendlist (reverselistslow xs) (Cons x Nil) Programmieren in Haskell 11
Reverse reverselistslow :: List a -> List a reverselistslow Nil = Nil reverselistslow (Cons x xs) = appendlist (reverselistslow xs) (Cons x Nil) Beispiel: reverselistslow (enumfromtolist 1 3) => Cons 3 (Cons 2 (Cons 1 Nil)) Programmieren in Haskell 11
Jetzt alle! isortlist (reverselistslow (enumfromtolist 1 90)) => Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 (Cons 6 (Cons 7 (Cons 8 (Cons 9 (Cons 10 (Cons 11 (Cons 12 (Cons 13 (Cons 14 (Cons 15 (Cons 16 (Cons 17 (Cons 18 (Cons 19 (Cons 20 (Cons 21 (Cons 22 (Cons 23 (Cons 24 (Cons 25 (Cons 26 (Cons 27 (Cons 28 (Cons 29 (Cons 30 (Cons 31 (Cons 32 (Cons 33 (Cons 34 (Cons 35 (Cons 36 (Cons 37 (Cons 38 (Cons 39 (Cons 40 (Cons 41 (Cons 42 (Cons 43 (Cons 44 (Cons 45 (Cons 46 (Cons 47 (Cons 48 (Cons 49 (Cons 50 (Cons 51 (Cons 52 (Cons 53 (Cons 54 (Cons 55 (Cons 56 (Cons 57 (Cons 58 (Cons 59 (Cons 60 (Cons 61 (Cons 62 (Cons 63 (Cons 64 (Cons 65 (Cons 66 (Cons 67 (Cons 68 (Cons 69 (Cons 70 (Cons 71 (Cons 72 (Cons 73 (Cons 74 (Cons 75 (Cons 76 (Cons 77 (Cons 78 (Cons 79 (Cons 80 (Cons 81 (Cons 82 (Cons 83 (Cons 84 (Cons 85 (Cons 86 (Cons 87 (Cons 88 (Cons 89 (Cons 90 Nil))))))))))))))))))))))))))))))))))))))))))))))))))))))))))) )))))))))))))))))))))))))))))) Programmieren in Haskell 12
Take takelist :: Integral a => a -> List b -> List b takelist n xs n < 0 = error "takelist not defined for negative values" takelist n Nil = Nil takelist n (Cons x xs) n == 0 = Nil n > 0 = Cons x (takelist (n-1) xs) Programmieren in Haskell 13
Beispiele: takelist 3 (enumfromtolist 1 10) => Cons 1 (Cons 2 (Cons 3 Nil)) takelist 3 (enumfromtolist 1 2) => Cons 1 (Cons 2 Nil) takelist 0 (enumfromtolist 1 10) => Nil takelist (-5) (enumfromtolist 1 10) => Program error: takelist not defined for negative values Programmieren in Haskell 14
Drop droplist :: Integral a => a -> List b -> List b droplist n xs n < 0 = error "droplist not defined for negative values" droplist n Nil = Nil droplist n (Cons x xs) n == 0 = (Cons x xs) n > 0 = droplist (n-1) xs Programmieren in Haskell 15
Drop droplist :: Integral a => a -> List b -> List b droplist n xs n < 0 = error "droplist not defined for negative values" droplist n Nil = Nil droplist n (Cons x xs) n == 0 = (Cons x xs) n > 0 = droplist (n-1) xs Beispiele: droplist 3 (enumfromtolist 1 10) => Cons 4 (Cons 5 (Cons 6 (Cons 7 (Cons 8 (Cons 9 (Cons 10 Nil)))))) droplist 10 (enumfromtolist 1 5) => Nil Programmieren in Haskell 15
Map maplist :: (a -> b) -> List a -> List b maplist f Nil = Nil maplist f (Cons x xs) = Cons (f x) (maplist f xs) Programmieren in Haskell 16
Map maplist :: (a -> b) -> List a -> List b maplist f Nil = Nil maplist f (Cons x xs) = Cons (f x) (maplist f xs) Beispiel: maplist (+1) (enumfromtolist 1 4) Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil))) Programmieren in Haskell 16
Filter filterlist :: (a -> Bool) -> List a -> List a filterlist p Nil = Nil filterlist p (Cons x xs) p x = Cons x (filterlist p xs) otherwise = filterlist p xs Programmieren in Haskell 17
Filter filterlist :: (a -> Bool) -> List a -> List a filterlist p Nil = Nil filterlist p (Cons x xs) p x = Cons x (filterlist p xs) otherwise = filterlist p xs Beispiel: filterlist (>5) (enumfromtolist 1 10) => Cons 6 (Cons 7 (Cons 8 (Cons 9 (Cons 10 Nil)))) Programmieren in Haskell 17
Filter-Beispiel oddlist :: Integral a => List a -> List a oddlist = filterlist odd where odd x = x mod 2 /= 0 Beispiel: oddlist (enumfromtolist 1 10) => Cons 1 (Cons 3 (Cons 5 (Cons 7 (Cons 9 Nil)))) Programmieren in Haskell 18
Filter-Beispiel oddlist :: Integral a => List a -> List a oddlist = filterlist odd where odd x = x mod 2 /= 0 Beispiel: oddlist (enumfromtolist 1 10) => Cons 1 (Cons 3 (Cons 5 (Cons 7 (Cons 9 Nil)))) Alternative ohne filterlist oddlistboring :: Integral a => List a -> List a oddlistboring Nil = Nil oddlistboring (Cons x xs) x mod 2 /= 0 = Cons x (oddlistboring xs) otherwise = oddlistboring xs Programmieren in Haskell 18
Concat concatlist :: List (List a) -> List a concatlist Nil = Nil concatlist (Cons xs xss) = appendlist xs (concatlist xss) Programmieren in Haskell 19
Concat concatlist :: List (List a) -> List a concatlist Nil = Nil concatlist (Cons xs xss) = appendlist xs (concatlist xss) Beispiel: concatlist (Cons (enumfromtolist 1 3) (Cons (enumfromtolist 4 5) Nil)) => Cons 1 (Cons 2 (Cons 3 (Cons 4 (Cons 5 Nil)))) Programmieren in Haskell 19