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
 [ Tags | programming ]

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)
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.

 From: 2008-07-09 08:57 pm (UTC) (Link)
oh, shit, i never finished the perturbation thing. hmmm.
 From: 2008-07-09 09:02 pm (UTC) (Link)
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.