[Shootout-list] Mandelbrot in Guile scheme
Greg Buchholz
sleepingsquirrel@member.fsf.org
Wed, 5 Jan 2005 11:16:41 -0800 (PST)
--0-544410256-1104952601=:70893
Content-Type: text/plain; charset=us-ascii
Content-Id:
Content-Disposition: inline
Attached is an implementation of the Mandelbrot test for Guile. I'd
imagine it wouldn't be too hard to port to the other scheme compilers.
Greg Buchholz
__________________________________
Do you Yahoo!?
Read only the mail you want - Yahoo! Mail SpamGuard.
http://promotions.yahoo.com/new_mail
--0-544410256-1104952601=:70893
Content-Type: text/x-scheme; name="fractal.scm"
Content-Description: fractal.scm
Content-Disposition: inline; filename="fractal.scm"
#!/usr/bin/guile \
-e main -s
!#
;;; Mandelbrot in Guile scheme
;;; Greg Buchholz
(define (main args)
(let ((n (string->number (cadr args))))
(display "P4") (newline) (display n) (display " ") (display n) (newline)
(printPBM 0 0 n (points 0 0 n))))
(define (points x y n)
(if (= y n)
'()
(if (= x n)
(points 0 (+ y 1) n)
(cons (mandel (make-rectangular (- (* 2 (/ x n)) 1.5)
(- (* 2 (/ y n)) 1.0)) 0.0+0.0i 50)
(delay (points (+ x 1) y n))))))
(define (mandel c z iter)
(if (= iter 0)
1
(let ((n (+ (* z z) c)))
(if (> (magnitude n) 2.0)
0
(mandel c n (- iter 1))))))
(define (printPBM acc i n stream)
(cond ((null? stream) (display (acc->char acc i)))
((and (= (remainder i 8) 0) (not (= i 0)))
(begin (display (integer->char acc))
(printPBM (car stream) (+ 1 i) n (force (cdr stream)))))
((= i n) (begin (display (acc->char acc n))
(printPBM 0 0 n stream)))
(else (printPBM (+ (* 2 acc) (car stream)) (+ 1 i) n (force (cdr stream))))))
(define (pow2 n) (if (> n 0) (* 2 (pow2 (- n 1))) 1))
(define (acc->char acc x)
(integer->char (* acc (pow2 (remainder x 8)))))
--0-544410256-1104952601=:70893--