sicp-solutions/ex-2.05.scm
Petar Kapriš 1b2b2dfec1 Add solutions to exercises from section 2.1
To be noted: the last two exercises, 2.15 and 2.16 were left unfinished,
and will be left for another time.
2025-02-05 15:17:47 +01:00

29 lines
505 B
Scheme

#lang sicp
(define (cons a b)
(* (expt 2 a)
(expt 3 b)))
(define (car pair)
(if (= (remainder pair 3) 0)
(car (/ pair 3))
(log2 pair)))
(define (cdr pair)
(if (= (remainder pair 2) 0)
(cdr (/ pair 2))
(log3 pair)))
(define (log2 num)
(define (aux num acc)
(if (<= num 1)
acc
(aux (/ num 2) (+ acc 1))))
(aux num 0))
(define (log3 num)
(define (aux num acc)
(if (<= num 1)
acc
(aux (/ num 3) (+ acc 1))))
(aux num 0))