dev-serveez
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [dev-serveez] complex guile server test


From: Martin Grabmueller
Subject: Re: [dev-serveez] complex guile server test
Date: Tue, 06 Nov 2001 11:14:19 +0100

> From: stefan <address@hidden>
> Date: Mon, 5 Nov 2001 19:55:03 +0100 (CET)
> 
> Martin: Is this any good/acceptable Guile style?  When looking at the
> *.scm files in ice-9 I start shivering.  The above code is rather C
> thinking and straight forward.  Could you please comment?

My comments are mainly stylistic, and it's of course up to you to
adopt common Scheme style or not.

> ;; Mandelbrot set
> 
> (define epsilon 1e-4)
> (define MaxIteration (* 1 1024))
> (define Colors MaxIteration)
> (define xRes 100)
> (define yRes 100)
> (define Start (make-rectangular -2 -2))
> (define End   (make-rectangular +2 +2))

The above is okay, but Schemers normally don't use upper-case
characters at all.  I would rather write:

  (define epsilon 1e-4)
  (define max-iteration (* 1 1024))
  (define colors max-iteration)
  (define x-res 100)
  (define y-res 100)
  (define start (make-rectangular -2 -2))
  (define end   (make-rectangular +2 +2))

> (define (near z1 z2)
>   (and (< (abs (real-part (- z2 z1))) epsilon)
>        (< (abs (imag-part (- z2 z1))) epsilon)))

Perfect.

> (define (iterateMandel z)
>   (let* ((old-z z) (done #f) (i 0) (result (- MaxIteration 1)))
>     (do ((i 0 (+ i 1)))
>         ((or done (>= i MaxIteration)))
>       (set! z (+ z (* z z)))
>       (if (near z old-z)
>           (set! i (- MaxIteration 1)))
>       (if (> (magnitude z) 2)
>           (begin
>             (set! done #t)
>             (set! result i)))
>       (set! old-z z))
>     result))

In Scheme programs, you normally you don't see that many `set!'s.
That is because most Schemers write in functional style.  For these
complex loops, I'd prefer using `named let' instead of `do'.  I'm not
sure about the algorithm, but I think it can be simplified.

  (define (iterate-mandel z)
    (let loop ((i 0) (z (+ z (* z z))) (old-z z))
       (cond ((>= i max-iteration)
              i)
             ((near z old-z)
              (- max-iteration 1))
             ((> (magnitude z) 2)
              i)
             (else
              (loop (+ i 1) (+ z (* z z)) z)))))
            

> (define (doMandel z1 z2 x-res y-res colors)
>   (let* ((x-diff (real-part (- z2 z1))) 
>          (y-diff (imag-part (- z2 z1)))
>          (x-ratio (/ x-diff x-res))
>          (y-ratio (/ y-diff y-res))
>          (x 0)
>          (y 0)
>          (z z1)
>          (i 0) (n 0)
>          (vector (make-vector (* x-res y-res))))
> 
>     (set! y-diff (* 0+1i y-diff))
>     (set! y-ratio (* 0+1i y-ratio))
>     (set! z (+ z (+ (/ y-ratio 2) (/ x-ratio 2))))
>     (do ((x 0 (+ x 1)))
>         ((>= x x-res))
>       (do ((y 0 (+ y 1)))
>           ((>= y y-res))
>         (set! i (iterateMandel z))
>         (vector-set! vector n (modulo i colors))
>         (set! n (+ n 1))
>         (set! z (+ z y-ratio)))
>       (set! z (- (+ z x-ratio) y-diff)))
>     vector))

Same as for iterate-mandel, above, but I'm too lazy to rewrite this
one :)

> (define (pretty i)
>   (let ((str ""))
>     (if (< i 10)
>         (set! str "0")
>         (if (< i 100)
>             (set! str "")))
>     (display str) (display i)))

I'm not sure what this is supposed to do, I'd suggest this one:

  (define (pretty i)
    (display (if (< i 10) "0" ""))
    (display i))

> (define (printMandel Mandel x-res y-res)
>   (let* ((x 0) (y 0))
>     (display "Mandelbrot:\n")
>     (do ((y 0 (+ y 1)))
>         ((>= y y-res))
>       (do ((x 0 (+ x 1)))
>           ((>= x x-res))
>         (pretty (vector-ref Mandel (+ (* x-res y) x)))
>         (display " "))
>       (newline))))

This can also be simplified, because the outer `let*' is not necessary.

  (define (print-mandel mandel x-res y-res)
    (display "Mandelbrot:\n")
    (do ((y 0 (+ y 1)))
        ((>= y y-res))
      (do ((x 0 (+ x 1)))
          ((>= x x-res))
        (pretty (vector-ref mandel (+ (* x-res y) x)))
        (display " "))
      (newline)))

> (define t (current-time))
> (define Mandel (doMandel Start End xRes yRes Colors))
> (display (- (current-time) t)) (display " seconds --- ")
> 
> (printMandel Mandel xRes yRes)

Nothing to comment on :)

Hope that was instructive,
  'martin

PS: Thanks for hacking on Guile!



reply via email to

[Prev in Thread] Current Thread [Next in Thread]