Mandelbrot in scheme, now in Block Structure™

#!/usr/bin/env guile
!#

(define (mandelbrot left right bottom top)
  (define cols 78)
  (define rows 39)
  (define iter 36)
  (define (density n)
    (cond ((= n iter) #\ )
	  ((and (>= n 0) (<= n 9)) (integer->char (+ n 48)))
	  ((and (>= n 10) (<= n 36)) (integer->char (+ n 35 -10)))
	  (else #\.)))

  (define (mandelbrot left right bottom top)
    (do ((i 0 (1+ i))) ((>= i rows))
      (do ((j 0 (1+ j))) ((>= j cols))
	(let ((x (+ (* (/ j cols) (- right left)) left))
	      (y (+ (* (/ i rows) (- top bottom)) bottom)))
	  (write-char (density (let lp ((c (make-rectangular x y))
					(z 0+0i)
					(k 0))
				 (cond ((or (= k iter) (>= (magnitude z) 2)) k)
				       (else (lp c (+ (* z z) c) (1+ k )) )) ))) ))
      (newline)))
  (mandelbrot left right bottom top))

(mandelbrot -2 2 -2 2)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <p> <br> <pre>

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.