Very Short Mandelbrot Implementation in Haskell

I was a bit bored tonight when this idea came to my mind.
I already implemented the mandelbrot algorithm in Kotlin two years ago. In Haskell it's even simpler because of the builtin complex number functionality. I'm sure you could save some characters, but that's the smallest code that I was able to write in one evening and I also wanted to keep the code (somewhat) readable.

import Data.Complex (magnitude, Complex ((:+)))

main :: IO ()
main = putStrLn $ foldl (\a r -> a ++ r ++ "\n") "" (row <$> [-1,-0.9..1])
  where row r = concat $ (\c -> mandelbrot (c :+ r )) <$> [-2,-1.95..1]
        mandelbrot c = if (magnitude $ iterations c) > 2 then " " else "*"
        iterations c = (iterate (\z -> z^2 + c) (0.0 :+ 0.0)) !! 10
$ runghc mandelbrot.hs

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

Show Comments