Created
December 26, 2014 04:19
-
-
Save zerovolts/74d7a3d9a7a075560e1c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data Complex a = Complex a a deriving (Show) | |
pretty :: (Show a) => Complex a -> String | |
pretty (Complex a b) = show a ++ " + " ++ show b ++ "i" | |
pp :: (Show a) => Complex a -> IO () | |
pp a = putStrLn . pretty $ a | |
cAdd :: (Floating a) => Complex a -> Complex a -> Complex a | |
cAdd (Complex a b) (Complex x y) = Complex (a + x) (b + y) | |
cMult :: (Floating a) => Complex a -> Complex a -> Complex a | |
cMult (Complex a b) (Complex x y) = Complex (a * x) (b * y) | |
cExp :: (Floating a) => Complex a -> a -> Complex a | |
cExp (Complex a b) e = Complex (a ** e) (b ** e) | |
cMagnitude :: (Floating a) => Complex a -> a | |
cMagnitude (Complex a b) = sqrt $ (a * a) + (b * b) | |
-- returns Nothing if stays below 2 | |
-- returns Just Int for how many iterations before escaping past 2 | |
_mandelbrot :: (Ord a, Floating a, Integral b) => Complex a -> Complex a -> b -> Maybe b | |
_mandelbrot _ _ 0 = Nothing | |
_mandelbrot (Complex zr zi) (Complex cr ci) iter | |
| mag > 2 = Just iter | |
| mag <= 2 = _mandelbrot (((Complex zr zi) `cExp` 2) `cAdd` (Complex cr ci)) (Complex cr ci) (iter - 1) | |
where mag = cMagnitude (Complex zr zi) | |
mandelbrot :: (Ord a, Floating a, Integral b) => Complex a -> b -> Maybe b | |
mandelbrot (Complex cr ci) iter = _mandelbrot (Complex cr ci) (Complex cr ci) iter | |
main = do | |
print $ mandelbrot (Complex (-1) 0.5) 50 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment