Log in

No account? Create an account
improver? I hardly KNOW 'er! - Lindsey Kuper [entries|archive|friends|userinfo]
Lindsey Kuper

[ website | composition.al ]
[ userinfo | livejournal userinfo ]
[ archive | journal archive ]

improver? I hardly KNOW 'er! [Jul. 9th, 2008|03:43 pm]
Lindsey Kuper

Today and yesterday have been amazing for SICP checkins. I've finished 17 problems in the last 48 hours! Here's my answer to the last problem in chapter 1:

;;;; 1.46
;; ...Write a procedure /iterative-improve/ that takes two procedures as
;; arguments: a method for telling whether a guess is good enough and a method
;; for improving a guess.  /Iterative-improve/ should return as its value a
;; procedure that takes a guess as argument and keeps improving the guess
;; until it is good enough. Rewrite the /sqrt/ procedure of section 1.1.7 and
;; the /fixed-point/ procedure of section 1.3.3 in terms of
;; /iterative-improve/.

(define iterative-improve
  (lambda (good-enough? improve)
    (letrec ((improver (lambda (guess)
                         (if (good-enough? guess)
                             (improver (improve guess))))))
      (lambda (guess)
        (improver guess)))))

(define sqrt-using-iterative-improve
  (lambda (x)
    (define (improve guess)
      (average guess (/ x guess)))
    (define (good-enough? guess)
      (< (abs (- (square guess) x)) 0.001))
    ((iterative-improve good-enough? improve) 1.0)))

(define fixed-point-using-iterative-improve
  (lambda (f first-guess)
    (define (next guess)
      (f guess))
    (define (close-enough? guess)
      (< (abs (- guess (next guess))) tolerance))
    ((iterative-improve close-enough? next) first-guess)))

I just love writing stuff that looks like this, especially iterative-improve. Why write code that does something when you can write code that does anything? Whee!

There's just one problem left to finish -- the second half of this one. It's sticky, but I think I can get it done before the start of ICFP. You know, even if the contest is a wash, I'll feel good going into it knowing I can do all the exercises in the first chapter of this book. I feel so much stronger than I did four months ago.


[User Picture]From: catechism
2008-07-09 08:57 pm (UTC)
oh, shit, i never finished the perturbation thing. hmmm.
(Reply) (Thread)
[User Picture]From: jes5199
2008-07-09 09:02 pm (UTC)
you might like Haskell, then (or anything in the ML family). Instead of using "map" to map over lists, you can use "fmap" for any kind of data that implements the Functor interface. instead of "foldl" to reduce lists, you can use "foldl'" to reduce anything that implements the Foldable interface.

conversely! I've been reading about Defunctionalization: the refactoring method of taking the magic out to see what's really going on

someone on the pdxfunc mailing list called last year's ICFP "very hard, probably the most difficult one ever."
I intend to kick their ass, programmatically.
(Reply) (Thread)
[User Picture]From: lindseykuper
2008-07-09 09:25 pm (UTC)
I know, I know. A few months ago, Sam told me, "You need to learn Haskell." There aren't that many things that Sam Rebelsky tells me I need to do, y'know? And Paul says, "Lindsey, I tell everyone to learn Haskell, but in your case, I really mean it!"

It really is on my list. I intend to take a break from Scheme after the contest is over. I feel like the end of chapter 1 is a good stopping point for now. I want to keep going with it, but there's just so much other stuff to learn.
(Reply) (Parent) (Thread)
[User Picture]From: gorthx
2008-07-09 09:23 pm (UTC)
You are my hero(ine)!
(Reply) (Thread)
[User Picture]From: themall
2008-07-10 02:41 am (UTC)
this is pretty awesome. you've inspired me to do the same thing.
(Reply) (Thread)