import Data.Char double :: Integer -> Integer double x = x + x doubleEven :: Integer -> Integer doubleEven x = if even x then double x else x myEven x = x `mod` 2 == 0 doubleEven' a | even a = double a | otherwise = a doubleLam = \x -> x + x applyAndAdd f x y = (f x) + (f y) twice :: (a -> a) -> a -> a twice f x = f (f x) fakultaet x = if x <= 1 then 1 -- Rekursionsanfang else x*(fakultaet (x-1)) -- Rekursionsschritt double_n_times :: Integer -> Integer -> Integer double_n_times x n = if n == 0 then x else double_n_times (double x) (n-1) double_n_times2 :: Integer -> Integer -> Integer double_n_times2 x 0 = x double_n_times2 x n = double_n_times2 (double x) (n-1) double_n_times3 :: Integer -> Integer -> Integer double_n_times3 x n | n == 0 = x | otherwise = double_n_times3 (double x) (n-1) double_n_times4 :: Integer -> Integer -> Integer double_n_times4 x n | n < 0 = error "Negatives Verdoppeln verboten!" | n == 0 = x | otherwise = double_n_times4 (double x) (n-1) echo :: IO () echo = do line <- getLine putStrLn line copy :: FilePath -> FilePath -> IO () copy from to = do content <- readFile from writeFile to content cat :: FilePath -> IO () cat file = do content <- readFile file putStrLn content copyAndUpper from to = do content <- readFile from writeFile to (map toUpper content) myHead [] = error "empty list" myHead (x:xs) = x isEmpty [] = True isEmpty (x:xs) = False isEmpty2 [] = True isEmpty2 xs = False last2 [] = error "leere Liste" last2 (x:[]) = x last2 (x:xs) = last2 xs heads :: [[a]] -> [a] heads [] = [] heads ((x:xs):ys) = x:(heads ys) heads ([]:ys) = heads ys map1 :: (a -> b) -> [a] -> [b] map1 f [] = [] map1 f (x:xs)= (f x):(map1 f xs) filter1 :: (a -> Bool) -> [a] -> [a] filter1 p [] = [] filter1 p (x:xs) | p x = x:(filter1 p xs) | otherwise = filter1 p xs numWords :: String -> Int numWords text = length (words text) eigenesFst (x,y) = x eigenesSnd (x,y) = y paarSumme (x,y) = x+y erstes_aus_vier_tupel (w,x,y,z) = w viertes_aus_vier_tupel (w,x,y,z) = z hanoi 1 start ziel hilf = [(start,ziel)] -- Allgemeiner Fall: hanoi n start ziel hilf = -- Schiebe den Turm der Hoehe n-1 von start zu hilf: (hanoi (n-1) start hilf ziel) ++ -- Schiebe n. Scheibe von start auf ziel: [(start,ziel)] ++ -- Schiebe Turm der Hoehe n-1 von hilf auf ziel: (hanoi (n-1) hilf ziel start) start_hanoi n = hanoi n "S" "Z" "H" beispiel_list_compr = [(x*x,y) | x <- [1..5] , y <- "aBcDeF" , even x , isUpper y] -- max2 a b | a >= b = a | otherwise = b max3 a b c = if a >= b then if a >= c then a else c else if b >= c then b else c max3 a b c | a >= b && a >= c = a | b >= a && b >= c = b | otherwise = c max3 a b c = max2 (max2 a b) c max3 a b c = let max2 x y = if x >= y then x else y maxab = max2 a b in max2 maxab c max3 a b c = max2 maxab c where max2 x y | x >= y = y | otherwise = y maxab = max a b quersumme x = if x `div` 10 == 0 then x `mod` 10 -- Zahl ist einstellig else (x `mod` 10) + (quersumme (x `div` 10)) quersumme' x = let (d,r) = divMod x 10 in if d == 0 then r else r + (quersumme' d) -- Normaler Palindromtest pal xs = reverse xs == xs -- Entfernen von Leer- und Satzzeichen remove = filter isAlphaNum -- alles Kleinschreiben tolower = map toLower spal = pal . tolower . remove