Fractals are one of the prettier applications of mathematics. A fractal is an infinitely detailed figure that contains copies of itself at higher magnification levels. Easily the most well-known fractal is the Mandelbrot set, named after its discoverer BenoĆ®t Mandelbrot, who also coined the term fractal.

The Mandelbrot set is defined as all the points c on the complex plane for which the absolute value of z_{n} remains less than or equal to 2, where z_{n} is defined as:

z_{0} = c

z_{n+1} = z_{n}^{2} + c

While technically n should be allowed to run to infinity, this is obviously not very practical, so an arbitrary n is chosen, with higher values obviously being more accurate.

Your task in today’s exercise is to write a very simple visualizer for the Mandelbrot set; none of the fancy colors from more advanced implementations, just plain old ascii symbols. When you are finished, you are welcome to read a suggested solution below, or to post your own solution or discuss the exercise in the comments below (to post code, put it between [code][/code] tags).

Since the Mandelbrot set is defined on the complex plane, we’re going to be working with complex numbers.

import Data.Complex

Since we’re not exactly working at a very high level of detail, 50 iterations will be plenty.

mandelbrot :: Num a => a -> a mandelbrot c = iterate (\z -> z^2 + c) c !! 50

Generating the Mandelbrot set is a simple matter of generating a series of points between the given limits using the given resolutions and testing them, assigning stars to the points in the set and spaces to those outside.

display :: Float -> Float -> Float -> Float -> Float -> Float -> String display x1 x2 resx y1 y2 resy = unlines [[if magnitude (mandelbrot (x :+ y)) <= 2 then '*' else ' ' | x <- [x1, x1 + resx .. x2]] | y <- [y2, y2 - resy .. y1]]

If we choose the correct boundaries we will see the familiar shape of the Mandelbrot set appear.

main :: IO () main = putStrLn $ display (-2.1) 0.5 0.025 (-1) 1 0.05

** ********* ******** ******** * * ********** * ** ******************** ******************************* *** *********************************** ** *********************************** ************************************** ********************************************* ****************************************** ** ****** ********************************************* ************* ******************************************** ***************** ********************************************* ******************* ******************************************** **** *************************************************************** ****************************************************************************************** **** *************************************************************** ******************* ******************************************** ***************** ********************************************* ************* ******************************************** ** ****** ********************************************* ****************************************** ********************************************* ************************************** ** *********************************** *********************************** ******************************* *** ** ******************** * * ********** * ******** ******** ********* **

Sure, it’s rudimentary, but then again it’s only four lines.