module FFT ( fft ) where import Data.Complex import System.Random cmpExp n k j = mkPolar 1 (-2 * pi * fromIntegral k / fromIntegral n) * j splitEvenOdd [] = ([], []) splitEvenOdd [x] = ([x],[]) splitEvenOdd (x:y:xs) = (x:xe, y:yo) where (xe, yo) = splitEvenOdd xs combine [] [] = [] combine xs [] = xs combine [] ys = ys combine xs ys = add ++ sub where add = zipWith (+) xs ys sub = zipWith (-) xs ys fft :: [Complex Double] -> [Complex Double] fft [] = [] fft [x] = [x] fft xs = combine even cmpOdd where (e, o) = splitEvenOdd xs even = fft e odd = fft o size = length xs cmpOdd = zipWith (cmpExp size) [0..] odd createList :: Int -> [Complex Double] createList n = take (2^n) $ zipWith (:+) (randoms (mkStdGen 10)) (randoms (mkStdGen 10)) main :: IO() main = interact (show . fft . createList . read)