Programmieren in Haskell Programmieren mit Listen Peter Steffen Universit¨ at Bielefeld Technische Fakult¨ at
14.11.2008
1
Programmieren in Haskell
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]
2
Programmieren in Haskell
Spezifikation des Sortierproblems
Gew¨ unscht ist eine Funktion sortList :: Ord a => List a -> List a, so daß f¨ ur jede Liste xs :: List a (mit Ord a), die Elemente von sortList xs geordnet sind, keine Elemente hinzukommen und keine verlorengehen.
3
Programmieren in Haskell
Sortieren durch Einf¨ugen
isortList :: Ord a => List a -> List a isortList Nil = Nil isortList (Cons x xs) = insertList x (isortList xs) where insertList insertList | x y
x x = =
Nil = Cons x Nil (Cons y ys) Cons x (Cons y ys) Cons y (insertList x ys)
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
5
Programmieren in Haskell
L¨ange, 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
6
Programmieren in Haskell
L¨ange, 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
6
Programmieren in Haskell
L¨ange, 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
6
Programmieren in Haskell
Aufz¨ahlungen 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)
Aufz¨ahlungen 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)
Fakult¨at 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 :: factorial n | n < 0 = | n == 0 = | n > 0 =
8
Integral a => a -> a error "factorial not defined for negative values" 1 n * factorial (n-1)
Programmieren in Haskell
Fakult¨at 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 :: factorial n | n < 0 = | n == 0 = | n > 0 =
8
Integral a => a -> a error "factorial not defined for negative values" 1 n * factorial (n-1)
Programmieren in Haskell
Fakult¨at 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 :: factorial n | n < 0 = | n == 0 = | n > 0 =
8
Integral a => a -> a error "factorial not defined for negative values" 1 n * factorial (n-1)
Programmieren in Haskell
Append
appendList :: List a -> List a -> List a appendList Nil ys = ys appendList (Cons x xs) ys = Cons x (appendList xs ys)
takeList takeList | n < takeList takeList | n == | n >
11
:: Integral a => a -> List b -> List b n xs 0 = error "takeList not defined for negative values" n Nil = Nil n (Cons x xs) 0 = Nil 0 = Cons x (takeList (n-1) xs)
Drop dropList dropList | n < dropList dropList | n == | n >
:: Integral a => a -> List b -> List b n xs 0 = error "dropList not defined for negative values" n Nil = Nil n (Cons x xs) 0 = (Cons x xs) 0 = dropList (n-1) xs
Drop dropList dropList | n < dropList dropList | n == | n >
:: Integral a => a -> List b -> List b n xs 0 = error "dropList not defined for negative values" n Nil = Nil n (Cons x xs) 0 = (Cons x xs) 0 = dropList (n-1) xs
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
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