Monday, July 26, 2010

Thursday, July 15, 2010

Vacation!

I will not be working on SICP or school for the next week, as I will be in Yellowstone with my girlfriend. I am really looking forward to this trip and will hopefully have pictures and stories when I return.

Monday, July 12, 2010

SICP Exercise 1.11

SICP 1.11

 ; recursive version  
 (define (f-rec n)  
  (cond ((<= n 3) n)  
     (else (+ (f-rec (- n 1))   
          (* 2 (f-rec (- n 2)))   
          (* 3 (f-rec (- n 3)))))))  
   
 ; iterative version  
 (define (f n)  
  (cond ((<= n 3) n)  
     (else (f-iter 1 2 3 3 n))))  
   
 (define (f-iter fn-1 fn-2 fn-3 last-calc end)  
  (cond ((= last-calc end) fn-3)  
     (else (f-iter fn-2 fn-3 (+ fn-3 (* 2 fn-2) (* 3 fn-1)) (+ last-calc 1) end))))  
   

SICP Exercise 1.10

SICP 1.10

(A 1 10) = 1024
(A 2 4) = 65536
(A 3 3) = 65536

All of the following assume n =/= 0.
\[(f. n) = 2n\]

\[(g. n) = 2^n\]

\[(h. n) = 2^{(h (n-1))}\]

\[(k. n) = 5n^2\]

Tuesday, July 6, 2010

SICP Exercise 1.9

SICP 1.9

The first definition:
(+ 4 5)
(inc (+ 3 5))
(inc (inc (+ 2 5)))
(inc (inc (inc (+ 1 5))))
(inc (inc (inc (inc (+ 0 5)))))
(inc (inc (inc (inc 5))))
(inc (inc (inc 6)))
(inc (inc 7))
(inc 8)
9

The second definition:
(+ 4 5)
(+ 3 6)
(+ 2 7)
(+ 1 8)
(+ 0 9)
9

The first definition would be what the book calls a recursive process, and the second would be iterative.

Sunday, July 4, 2010

SICP Exercise 1.8

SICP 1.8

 
 (define (square x)  
  (* x x))

(define (cube x)  
  (* x x x))  

 (define (improve guess x)  
  (/ (+ (/ x (square guess)) (* 2 guess)) 3))
  
 (define (good-enough? old-guess new-guess)  
  (< (/ (abs (- new-guess old-guess)) new-guess) 0.001))  

 (define (cube-iter old-guess new-guess x)  
  (if (good-enough? old-guess new-guess)  
    new-guess  
    (cube-iter new-guess (improve new-guess x) x)))  

 (define (cube-root x)  
  (cube-iter 0 1 x))  

SICP Exercise 1.7

Instead of spending the time to write out every SICP problem I am going to start linking to them via the html book.

The obvious problem with very small numbers and very large numbers resides in the precision of only using 0.001 as the difference validator between changes. When you get very small numbers you start getting off hundreds of percent errors, even for relatively simple squares, such as 0.0000001 and 0.0000000000001. For very large numbers you run into the same issue. In fact, the lack of appropriate precision gets so ridiculous at high numbers that often the program fails to end due to an inability to decipher the right amount of change between guesses.

 (define (square x) (* x x))  

 (define (sqrt-iter old-guess new-guess x)  
  (if (good-enough? old-guess new-guess)  
    new-guess  
    (sqrt-iter new-guess (improve new-guess x)  
          x)))  

 (define (improve guess x)  
  (average guess (/ x guess)))  

 (define (average x y)  
  (/ (+ x y) 2))  

 (define (good-enough? old-guess new-guess)  
  (< (/ (abs (- new-guess old-guess)) new-guess) 0.0000001))  

 (define (sqrt x)  
  (sqrt-iter 0 1.0 x))  

This alleviates the previous errors and gives expected results.

Friday, July 2, 2010

Another Endless Workout

Despite being relatively late I was still able to get an awesome workout at Endless today. My trainer had me perform a general preparedness procedure workout. I performed:

  • lunges while holding an curled olympic bar back and forth the gym
  • kettlebell swings
  • box jumps (I need to ask how high these jumps were)
  • band pull-aparts
Overall a great workout. More "cardio" than most of mine, but still really tough.

Thursday, July 1, 2010

An update

I've had to set SICP aside to tackle some issues related to work and school. None the less I will return soon.

In the meantime I competed in an in-house jiu-jitsu tournament in my school this weekend. Normally I would not have done so given that I am injured, but two of our affiliation's black belts were there so I had hoped to show off. I did not give my best performance but I took 2nd, which is something I am proud of, tapping out people who are higher rank than myself all while my ribs were displaced.



Once work and school settles down I will return to SICP. I may even pick up a physical copy just for the incentive.