[Shootout-list] mandelbrot GHC
Greg Buchholz
sleepingsquirrel@member.fsf.org
Wed, 22 Dec 2004 09:49:32 -0800 (PST)
--0-1903032872-1103737772=:38478
Content-Type: text/plain; charset=us-ascii
Content-Id:
Content-Disposition: inline
Attached is a GHC version of the mandelbrot test in the sandbox.
Greg Buchholz
__________________________________
Do you Yahoo!?
Take Yahoo! Mail with you! Get it on your mobile phone.
http://mobile.yahoo.com/maildemo
--0-1903032872-1103737772=:38478
Content-Type: text/x-haskell; name="fractal.hs"
Content-Description: fractal.hs
Content-Disposition: inline; filename="fractal.hs"
-- Fractal.hs mandlebrot example
-- dumps a *.ppm graphics file to stdout
-- compile: ghc -O2 -o fractal fractal.hs
-- run: fractal 600 >mandel.pbm
-- by Greg Buchholz
import Complex
import System(getArgs)
import Char(chr)
main = do [arg] <- getArgs
let width = read arg
let pts = points width width
putStr $ "P4\n" ++ arg ++ " " ++ arg ++ "\n"
putStr $ concatMap (makePBM 0 0) (chunk width (fractal pts))
limit = 2
iter = 50+1 -- add one to compensate for the 'iterate' function
points width height = [(2*x/w - 1.5) :+ (2*y/h - 1) | y<-[0..h-1], x<-[0..w-1]]
where w = fromIntegral width
h = fromIntegral height
mandel c z = z * z + c
fractal pts = map (\f-> length (takeIter (iterate f (0:+0)))) (map mandel pts)
where takeIter a = take iter (takeWhile (\x-> magnitude(x)<limit) a)
makePBM i acc [] = (chr (acc * 2^(8-i))) : []
makePBM i acc (x:xs) | i==8 = (chr acc) : makePBM 0 0 (x:xs)
| otherwise = makePBM (i+1) n xs
where
n = if x==iter then (acc*2+1) else (acc*2)
chunk width [] = []
chunk width c = (fst split) : chunk width (snd split)
where split = splitAt width c
--0-1903032872-1103737772=:38478--