Isn't that just the way of it...

I mentioned in the previous entry that I was missing Python's List Comprehensions. Well, I've gotten a little closer to having them. For some reason, LispMe doesn't come with a zip method, and you can only get map by importing a "Standard Library" memo. So, I had to write my own version of zip, and here it is, for anyone else who might find it useful.

1
2
3
4
(define (zip s1 s2)
  (if (or (null? s1) (null? s2)) '()
    (cons (list (car s1) (car s2))
      (z (cdr s1) (cdr s2)))))

Along with my halfway function, redefined to be (define (halfway x) (/ (+ (car x) (cadr x)) 2)), I can now write (map halfway (zip current-point next-point)) to get the point halfway between where I am, and where I am going to.

I also came to another realization. I was planning on defining a current-point, and using set! to update it to the new halfway point, but when I think about it, I don't really care what the current point is at any time other than processing, so there's no particular need to set it, and instead I should just pass it around as a parameter, making my iter function (and I just made up the name "iter function", based on the <a href= "http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-10.html#%_sec_1.1.7"

sqrt-iter function in "Structure and Interpretation of Computer Programs") look something like this:

1
2
3
4
5
6
(define (triangle-iter start num-iters)
  (if (positive? num-iters)
    (begin (draw-point start)
    (triangle-iter
      (map halfway (zip current-point next-point))
      (- num-iters 1)))))

Does that look appropriately Scheme-ish, do you think?

Comments

Comments powered by Disqus