import Prelude hiding (all,foldl,length) import Monad (when) import List(intersperse) import Debug.Trace (trace) import Hugs.Memo (memo) data Item = Item String Int Int data Book' = Book' String String String String getSign :: Book'-> String getSign (Book' _ _ _ s) = s setSign :: Book'-> String-> Book' setSign (Book' t a p _) s = Book' t a p s data Book = Book { author :: String, title :: String, publisher :: String } b = Book {author = "M. Proust", title = "A la recherche du temps perdu", publisher = "S. Fischer Verlag"} print :: Book-> IO () print (Book{author= a, publisher= p, title= t}) = putStrLn (a++ " schrieb "++ t ++ " und "++ p++ " veröffentlichte es.") b2 = Book {author= "C. Lüth"} shortPrint :: Book-> IO () shortPrint (Book{title= t, author= a}) = putStrLn (a++ " schrieb "++ t) fac' :: Integer-> Integer fac' n = if n == 0 then 1 else n * fac' (n-1) fac :: Integer-> Integer fac n = fac0 n 1 where fac0 n acc = if n == 0 then acc else fac0 (n-1) (n*acc) rev' :: [a]-> [a] rev' [] = [] rev' (x:xs) = rev' xs ++ [x] rev :: [a]-> [a] rev xs = rev0 xs [] where rev0 [] ys = ys rev0 (x:xs) ys = rev0 xs (x:ys) length' :: [a]-> Int length' xs = if (null xs) then 0 else 1+ length' (tail xs) length :: [a]-> Int length xs = len xs 0 where len xs y = if (null xs) then 0 + y else len (tail xs) (1+ y) getLines' :: IO String getLines' = do str<- getLine if null str then return "" else do rest<- getLines' return (str++ rest) getLines :: IO String getLines = getit "" where getit res = do str<- getLine if null str then return res else getit (res++ str) data Set a = Set [a] -- Mengen als Listen instance Show a=> Show (Set a) where showsPrec i (Set elems) = showElems elems where showElems [] = ("{}" ++) showElems (x:xs) = ('{' :) . shows x . showl xs where showl [] = ('}' :) showl (x:xs) = (',' :) . shows x . showl xs fac2 n = fac0 n 1 where fac0 n acc = seq acc $ if n == 0 then acc else fac0 (n-1) (n*acc) foldl :: (a -> b -> a) -> a -> [b] -> a foldl f z [] = z foldl f z (x:xs) = foldl f (f z x) xs all :: (a-> Bool)-> [a]-> Bool all p = foldr ((&&) . p) True f :: Int-> Int f x = (x+ 1)* (x+ 1) fibsFn :: () -> [Integer] fibsFn () = 1 : 1 : zipWith (+) (fibsFn ()) (tail (fibsFn ())) fibsFn' :: () -> [Integer] fibsFn' = memo (\()-> 1 : 1 : zipWith (+) (fibsFn' ()) (tail (fibsFn' ()))) facs n = if n == 0 then 1 else n* facs (n-1)