<-- Back to Andy Balaam Home

Haskellbrot

Haskellbrot draws a Mandelbrot set (or a Julia or "multibrot" set) to your console window. It's unlikely to be any use to you, unless you're interested in how a complete Haskell beginner writes basic Haskell.

The output looks something like this:

To compile it you will need GHC. On Ubuntu, this will install it:

sudo apt-get install ghc6

If you save the code as Haskellbrot.hs you can compile and run like this:

ghc --make Haskellbrot.hs
./Haskellbrot

Here is the code:

{-----------------------------}

import Complex

{-----------------------------}

get_increment :: (RealFloat a) => a -> a -> Int -> a
get_increment min max reg = (max-min) / ( fromIntegral reg )

generate_coords_row :: (RealFloat a) => a -> a -> Int -> a -> [Complex a]
generate_coords_row min_r max_r reg_r current_c = take (reg_r+1) (
		iterate ( + ( ( get_increment min_r max_r reg_r ) :+ 0 ) ) ( min_r :+ current_c )
	)

generate_coords_complex :: (RealFloat a) => a -> a -> Int -> [a]
generate_coords_complex min_c max_c reg_c = take (reg_c+1) (
		iterate ( + ( get_increment min_c max_c reg_c ) ) ( min_c ) )

generate_coords_array :: (RealFloat a) => a -> a -> a -> a -> Int -> Int -> [Complex a]
generate_coords_array min_r max_r min_c max_c reg_r reg_c = foldl1 (++) (
	map ( generate_coords_row min_r max_r reg_r ) (
		generate_coords_complex min_c max_c reg_c ) )

{-----------------------------}

in_or_out :: Int -> String
in_or_out (-1) = "*"
in_or_out  x   = " "

get_fractal_row :: [Int] -> String
get_fractal_row a = foldl1 (++) ( map in_or_out a )

impl_print_fractal :: Int -> ( [Int], [Int] ) -> IO()
impl_print_fractal row_width ([], b)  = impl_print_fractal row_width ( splitAt row_width b )
impl_print_fractal _         (a, []) = putStrLn( get_fractal_row a )
impl_print_fractal row_width (a,  b)  = do {
		putStrLn( get_fractal_row a );
		impl_print_fractal row_width ( splitAt row_width b )
	}

{- Given a width in columns, and an array of numbers
   (-1 for in the set, >-1 for outside),
   draw a mandelbrot set on the console.
-}
print_fractal :: Int -> [Int] -> IO()
print_fractal row_width array = impl_print_fractal row_width ( [], array )

{-----------------------------}

{- Perform one Mandelbrot interator z -> z^2 + c -}
multibrot_julia_iterate :: (RealFloat a) => Int-> Complex a -> Complex a -> Complex a
multibrot_julia_iterate pow c z = z^pow + c

{- Decide the fate of one pair z, c, using count iterations.
   return of -1 means inside the set,
   any other value is the number of iterations left before we would have declared it inside -}
multibrot_julia_decide :: (RealFloat a) => Int-> Int -> Complex a -> Complex a -> Int
multibrot_julia_decide pow count _ (x:+y) | x > 1  = count
multibrot_julia_decide pow count _ (x:+y) | y > 1  = count
multibrot_julia_decide pow count _ (x:+y) | x < -1 = count
multibrot_julia_decide pow count _ (x:+y) | y < -1 = count
multibrot_julia_decide pow 0     c z  = -1
multibrot_julia_decide pow count c z  = multibrot_julia_decide pow ( count-1 ) c ( multibrot_julia_iterate pow c z )

{- Decide the fate of a value of c in the Mandelbrot set -}
multibrot_decide :: (RealFloat a) => Int -> Int -> Complex a -> Int
multibrot_decide pow count c = multibrot_julia_decide pow count c ( 0 :+ 0 )

{-----------------------------}

{- Draw a "multibrot" set - where z is raised to a different power (not 2) -}
draw_multibrot :: (RealFloat a) => Int -> a -> a -> a -> a -> Int -> Int -> Int -> IO()
draw_multibrot pow min_r max_r min_c max_c cols rows iterations  =
	print_fractal (cols+1) ( map ( multibrot_decide pow iterations ) ( generate_coords_array min_r max_r min_c max_c cols rows ) )

{- Draw a Mandelbrot set -}
draw_mandelbrot :: (RealFloat a) => a -> a -> a -> a -> Int -> Int -> Int -> IO()
draw_mandelbrot = draw_multibrot 2

{- Draw a Julia set -}
draw_julia :: (RealFloat a) => a -> a -> a -> a -> Int -> Int -> Int -> Complex a -> IO()
draw_julia min_r max_r min_c max_c cols rows iterations fixed_c =
	print_fractal (cols+1) ( map ( multibrot_julia_decide 2 iterations fixed_c ) ( generate_coords_array min_r max_r min_c max_c cols rows ) )

main :: IO ()
main = draw_mandelbrot (-1.3) 0.75 (-0.9) 0.9 78 40 200			-- Full Mandelbrot set
--main = draw_julia (-1.2) 1.2 (-1.1) 1.1 78 40 200 (0.2 :+ 0.545)	-- Full Julia at (0.2 + 0.545i)
--main = draw_multibrot 3 (-1.2) 0.75 (-1.1) 1.1 78 40 200		-- Full "multibrot" with d=3
--main = draw_multibrot 4 (-1.2) 0.75 (-1.1) 1.1 78 40 200		-- Full "multibrot" with d=4
--main = draw_mandelbrot (-0.8) (-0.7) (-0.2) 0.2 78 40 200		-- Zoomed in on left hand side in middle

This code is made available under the terms of the GPLv2, or, at your discretion, any later version:

    Haskellbrot - draw a mandelbrot or related fractal to the console
    Copyright (C) 2008 Andy Balaam

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Edit | History | Print | Recent Changes | Search | Admin Page last modified on February 12, 2011, at 04:21 AM