Skip to content

Instantly share code, notes, and snippets.

@prescod
Created February 16, 2026 22:49
Show Gist options
  • Select an option

  • Save prescod/f7824318750b2798b119aa8372f26962 to your computer and use it in GitHub Desktop.

Select an option

Save prescod/f7824318750b2798b119aa8372f26962 to your computer and use it in GitHub Desktop.
Mandelbrot Runner in BechML
use Core.*;
scale : int := 1000;
xMin : int := Int.sub 0 2500;
xMax : int := 1000;
yMin : int := Int.sub 0 1750;
yMax : int := 1750;
width : int := 80;
height : int := 80;
maxIter : int := 50;
escapeSquared : int := 4000000;
getChar : int -> string := \n ->
if Int.eq n maxIter then "#"
else if Int.lt n 5 then " "
else if Int.lt n 10 then "."
else if Int.lt n 15 then ":"
else if Int.lt n 20 then "-"
else if Int.lt n 25 then "="
else if Int.lt n 30 then "o"
else if Int.lt n 35 then "O"
else if Int.lt n 40 then "@"
else "#";
squaredScaled : int -> int := \x -> Int.div (Int.mul x x) scale;
mandelbrotStep : int -> int -> int -> int -> int -> int := \cRe cIm zRe zIm iter ->
if Int.geq iter maxIter then maxIter
else
(\zReSquared zImSquared ->
(\magnitude ->
if Int.gt magnitude escapeSquared then iter
else
(\newRe newIm ->
mandelbrotStep cRe cIm newRe newIm (Int.add iter 1)
) (Int.add (Int.sub zReSquared zImSquared) cRe)
(Int.add (Int.div (Int.mul (Int.mul 2 zRe) zIm) scale) cIm)
) (Int.add zReSquared zImSquared)
) (squaredScaled zRe) (squaredScaled zIm);
computePoint : int -> int -> int := \cRe cIm ->
mandelbrotStep cRe cIm 0 0 0;
xRange : int := Int.sub xMax xMin;
yRange : int := Int.sub yMax yMin;
printPixel : int -> int -> io () := \y col ->
(\cRe cIm ->
(\iters ->
IO.print (getChar iters)
) (computePoint cRe cIm)
) (Int.add xMin (Int.div (Int.mul col xRange) width))
(Int.add yMin (Int.div (Int.mul y yRange) height));
printRowPixels : int -> int -> io () := \y col ->
if Int.geq col width then IO.print ""
else
IO.monad.bind (printPixel y col) (\_ ->
printRowPixels y (Int.add col 1));
printRows : int -> io () := \y ->
if Int.geq y height then IO.applicative.pure ()
else
IO.monad.bind (printRowPixels y 0) (\_ ->
printRows (Int.add y 1));
main : io () := printRows 0;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment