import Data.Char
import Prelude hiding (sum,and,foldr,foldl)
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)
powAcc b e acc
| e == 0 = acc
| even e = powAcc (b * b) (e `div` 2) acc
| otherwise = powAcc (b * b) (e `div` 2) (b * acc)
pow3 b e
| e < 0 = error "Exponent negativ"
| otherwise = powAcc b e 1
root e r
| e < 1 = error "Wurzelexponent zu klein"
| r < 0 = error "Zu kleiner Wert"
| otherwise = helper 0 (r+1)
where helper a b
| b - a == 1 = a
| pow3 half e > r = helper a half
| otherwise = helper half b
where half = (a + b) `div` 2
someDivs n = [x | x <- [2..(root 2 n)], n `mod` x == 0]
isPrime n
| n < 2 = False
| otherwise = null (someDivs n)
--merge xs ys
-- | null xs = ys
-- | null ys = xs
-- | head xs <= head ys = head xs : merge (tail xs) ys
-- | otherwise = head ys : merge xs (tail ys)
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 left) (mergeSort right)
where half = length xs `div` 2
(left, right) = splitAt half xs
f = let x = 3 in x where x = 4
--sum [] = 0
--sum (x:xs) = x + sum xs
sum xs = foldr (+) 0 xs
--and [] = True
--and (x:xs) = x && and xs
andR xs = foldr (&&) True xs
andL xs = foldl (&&) True xs
foldr op i [] = i
foldr op i (x:xs) = op x (foldr op i xs)
foldl op i [] = i
foldl op i (x:xs) = foldl op (op i x) xs