Question

I was wondering if anyone had any advice on writing the mandelbrot stream. I have wrote the following functions for myself to help:

(define (make-complex a b) (cons a b))
(define (real-coeff c) (car c))
(define (imag-coeff c) (cdr c))
(define (c-add c d)
      (make-complex (+ (real-coeff c) (real-coeff d))
                   (+ (imag-coeff c) (imag-coeff d))))
(define (c-mult c d)
    (make-complex (- (* (real-coeff c) (real-coeff d))
                  (* (imag-coeff c) (imag-coeff d)))
               (+ (* (real-coeff c) (imag-coeff d))
                  (* (imag-coeff c) (real-coeff d)))))
(define (c-length c)
   (define (square x) (* x x))
        (sqrt (+ (square (real-coeff c))
              (square (imag-coeff c)))))

I have that fz(x) = x2 +z. The stream should return: a, fz(a), fz(fz(a)), fz(fz(fz(a))). I am confused on how to use the functions that I wrote to create a stream that has this output. Anyone have some good advice as to where to go with this?

Was it helpful?

Solution

Start with a value for z and make your function fz(x) like:

(define (make-fz z) (lambda (x) (+ z (* 2 x))))

Now, using srfi-41 stream library, define a stream just as you've indicated: Try it out (with z of 0):

> (stream->list (stream-take 10 (stream-iterate (make-fz 0) 1)))
(1 2 4 8 16 32 64 128 256 512)

Note: that stream-iterate is defined something like:

(define-stream (stream-iterate fz a)
  (stream-cons a (stream-iterate fz (fz a))))

OTHER TIPS

As uselpa said, Scheme has built-in complex numbers. The functions you mentioned are provided as follows:

  • make-rectangular
  • real-part
  • imag-part
  • +
  • *
  • magnitude

As for the second part of your question, what is z? It's hard to answer this without knowing what you're wanting.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top