--module Arithmetik where
--import Prelude hiding (sum)
pow1 b 0 = 1
pow1 b e = b * pow1 b (e - 1)
pow2 b 0 = 1
pow2 b e
| even e = pow2 (b * b) (e `div` 2)
| otherwise = b * pow2 (b * b) (e `div` 2)
f a b c d = a + b + c + d
--pow3 = pow3acc 1
pow3 b e
| e < 0 = error "Exponent zu klein"
| otherwise = pow3acc 1 b e
where
pow3acc acc b 0 = acc
pow3acc acc b e
| even e = pow3acc acc (b * b) (e `div` 2)
| otherwise = pow3acc (acc * b) (b * b) (e `div` 2)
sum' [] = 0
sum' (x:xs) = x + sum' xs
and' [] = True
and' (x:xs) = x && and' xs
foldr' op init [] = init
foldr' op init (x:xs) = x `op` (foldr' op init xs)
foldl' op init [] = init
foldl' op init (x:xs) = foldl' op (init `op` x) xs
andr xs = foldr' (&&) True xs
andl xs = foldl' (&&) True xs
sum''' xs = foldr (+) 0 xs
sum'' xs = sumacc xs 0
where
sumacc [] acc = acc
sumacc (x:xs) acc = sumacc xs $! (x + acc)
main = do
putStrLn "Hello World"
print (sum'' [1..])
root e r
| e < 1 = error "Negativer Wurzelexponent"
| r < 0 = error "Negative Zahl"
| otherwise = searchRoot 0 (r+1)
where
searchRoot a b
| b - a == 1 = a
| pow3 m e <= r = searchRoot m b
| otherwise = searchRoot a m
where m = (a + b) `div` 2
isPrime n
| n < 2 = False
| otherwise = primeTest n (root 2 n) 2
where primeTest n bound current
| current > bound = True
| n `mod` current == 0 = False
| otherwise = primeTest n bound (current + 1)
isPrime' n
| n < 2 = False
| otherwise = null (filter (\k -> n `mod` k == 0) [2 .. root 2 n])
null' [] = True
null' _ = False
g xs@[x,_,z] = x : z : xs
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys)
| x < y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
mergeSort [] = []
mergeSort [x] = [x]
mergeSort xs = merge (mergeSort a) (mergeSort b)
where l = length xs `div` 2
(a,b) = splitAt l xs
--isSorted [] = True
--isSorted [x] = True
isSorted (x:xs@(y:_)) = x <= y && isSorted xs
isSorted _ = True
--mergeSortedIsSorted xs = isSorted (mergeSort xs)
mergeSortedIsSorted :: [Char] -> Bool
mergeSortedIsSorted = isSorted . mergeSort