; lob-add1 converts a list of bits into a list of bits
; representing the next larger integer. For instance, since 9+1=10,
; (list 1 0 0 1)) = (list 0 1 0 1)
;
; The code below was guided by the template,
; and the knowledge that:
; (0) + 1 = 1
; (2a + 0) + 1 = 2a + 1
; (2a + 1) + 1 = 2(a+1) + 0
(define lob-add1
(lambda (lob)
(if (null? lob)
(list 1)
(if (zero? (car lob))
(cons 1 (cdr lob))
(cons 0 (lob-add1 (cdr lob)))))))
; You could also use cond
instead of if
:
;
(define lob-add1
(lambda (lob)
(cond
((null? lob) (cons 1 null))
((zero? (car l)) (cons 1 (cdr lob)))
(else (cons 0 (lob-add1 (cdr lob)))))))