import Data.Numbers.Primes
import Text.Printf
merge [] ys = ys
merge xs [] = xs
merge (x:xs) (y:ys)
| x < y = x : merge xs (y:ys)
| otherwise = y : merge (x:xs) ys
primepowers n = mergeExp primes 1
where mergeExp ss i
| i == n + 1 = []
| otherwise = merge [s^i | s <- ss] $ mergeExp ss $ i + 1
primepowers' 1 = primes
primepowers' n = merge (map (^n) primes) (primepowers' (n - 1))
primepowers'' n = foldr merge [] [map (^i) primes | i <- [1..n]]
intersect [] bs = []
intersect as [] = []
intersect as@(a:as') bs@(b:bs')
| a == b = a : intersect as' bs'
| a < b = intersect as' bs
| otherwise = intersect as bs'
intersectAll [] = []
intersectAll [x] = x
intersectAll (x:xs) = intersect x $ intersectAll xs
--sum [] = 0
--sum (x:xs) = (+) x $ sum xs
intersectAll' :: (Ord t) => [[t]] -> [t]
intersectAll' [] = []
intersectAll' (x:xs) = foldr intersect x xs
commonMultiples as = intersectAll $ map (\a -> map (a *) [1..]) as
commonMultiples' = intersectAll [[2,4..],[3,6..],[7,14..]]
rand :: [Integer]
rand = x0 : map (\xi -> (a * xi + b) `mod` m) rand
where m = 2^16
a = 25173
b = 13849
x0 = 32
rand' = iterate (\xi -> (a * xi + b) `mod` m) x0
where m = 2^16
a = 25173
b = 13849
x0 = 32
-- Ansonsten bekannt als takeWhile
takeSome f [] = []
takeSome f (x:xs)
| f x = x : takeSome f xs
| otherwise = []
toCoin rs = map (\r -> r `mod` 300 >= 150) rs
neumann (x:y:xs)
| x == y = neumann xs
| otherwise = x : neumann xs
--bias (b:b':bs)
-- | b == b' && b = b : bias bs
-- | otherwise = False : bias bs
bias (b:b':bs) = (b && b') : bias bs
data Tree = Node Tree Int Tree | Leaf
instance Show Tree where
show Leaf = "L"
--show (Node t1 x t2) = "N (" ++ show t1 ++ ") " ++ show x ++ " (" ++ show t2 ++ ")"
show (Node t1 x t2) = printf "N (%s) %d (%s)" (show t1) x (show t2)