You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
906 B
42 lines
906 B
9 years ago
|
(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))
|