Browse Source

sicp notes ?? ??? ??

master
Taylor Bockman 9 years ago
parent
commit
fb7bfc7434
  1. 41
      sicp-2.5-notes

41
sicp-2.5-notes

@ -1,41 +0,0 @@
(car (cons x y))
(car (lambda (m) (m x y)))
((lambda (m) (m x y)) (lambda (p q) p)))
(lambda (x y) x)))
x
(define (cdr z) (z (lambda (x y) y)))
2.5 - Given that we know the bases - 2, and 3, we only need to find the exponents.
For example, 6 can only be represented one way - 2^1 3^1, meaning our pair of numbers a and b are 1
So, cons is simple:
(define (cons x y) (* (expt 2 x) (expt 3 y)))
Now we're left solving for a and b
log(2^a * 3^b) = alog(2) + blog(3)
call our "cons" value x
Because integer factorizations are unique we only need to find the biggest power of two and three that
go into the number.
(define (power-of-x value base)
(define (iter current-power)
(if (< ((expt base current-power) value)
(iter (+ current-power 1))
(current-power))))
Then with this we can write car and cdr as
(define (car int) (power-of-x int 2))
(define (cdr int) (power-of-x int 3))
Loading…
Cancel
Save