--http://en.wikipedia.org/wiki/Mandelbrot_set import System.Environment import Control.Monad (mapM_) xmin = -2 :: Double xmax = 1 :: Double ymin = -1 :: Double ymax = 1 :: Double escapesRec :: Int -> Double -> Double -> Double -> Double -> Bool escapesRec 0 _ _ _ _ = False escapesRec iter cr ci zr zi | (zr*zr + zi*zi) > 4 = True | otherwise = next_zr `seq` next_zi `seq` escapesRec (iter-1) cr ci next_zr next_zi where next_zr = zr*zr - zi*zi + cr next_zi = 2*zr*zi + ci escapes :: Int -> Double -> Double -> Bool escapes iter cr ci = escapesRec iter cr ci 0 0 row xs iters ci = map toChar [escapes (iters+1) cr ci | cr <- xs] where toChar b = if b then ' ' else 'X' rows xs ys iters = map (row xs iters) ys main = do args <- getArgs let xstep_str = args !! 0 let xstep = read xstep_str :: Int let ystep_str = args !! 1 let ystep = read ystep_str :: Int let iter_str = args !! 2 let iters = read iter_str :: Int -- row and column values (a little clunky but want to duplicate the other calculations exactly) let xs = map (\xc -> (fromIntegral xc)*(xmax-xmin)/(fromIntegral xstep) + xmin) [0..(xstep-1)] :: [Double] let ys = map (\yc -> (fromIntegral yc)*(ymax-ymin)/(fromIntegral ystep) + ymin) [0..(ystep-1)] :: [Double] mapM_ putStrLn $ rows xs ys iters