;; (define fun-for-2lob ;; (lambda (l1 l2) ;; (cond ;; [(and (null? l1) (null? l2)) ;; ...] ;; [(and (null? l1) (cons? l2)) ;; ...(car l2)...(cdr l2)...] ;; [(and (cons? l1) (null? l2)) ;; ...(car l1)...(cdr l1)...] ;; [(and (cons? l1) (cons? l2)) ;; ...(car l1)...(car l2)...(fun-for-2lob (cdr l1) (cdr l2))... ;; [else ;; (error "fun-for-2lob: Uh-oh, some case not handled" l1 l2)]))) ;; ;; Yes, some of the above cases could be simplified; ;; for instance the second condition could be just (null? l1) ;; since passing the previous line implies l1 and l2 aren't both null ;; so therefore (cons? l2) is clearly true. ;; However, that's a bit of thinking i prefer to leave to the computer ;; when possible. So i'll just blindly put down the four cases, ;; and not worry that any fancy reasoning i do might have a loophole. ;; (99.5% of the time my reasoning will be fine; but that .5% will ;; cause me hours of trouble searching for some obscure bug.) (define lob-add (lambda (l1 l2) (cond [(and (null? l1) (null? l2)) null] [(and (null? l1) (cons? l2)) l2] [(and (cons? l1) (null? l2)) l1] [(and (cons? l1) (cons? l2)) ; (cons (bit-sum-low (car l1) (car l2)) ; (if (zero? (bit-sum-carry (car l1) (car l2))) ; (lob-add (cdr l1) (cdr l2)) ; (lob-add1 (add (cdr l1) (cdr l2)))))] (cond [(and (zero? (car l1)) (zero? (car l2))) (cons 0 (lob-add (cdr l1) (cdr l2)))] [(and (zero? (car l1)) (one? (car l2))) (cons 1 (lob-add (cdr l1) (cdr l2)))] [(and (one? (car l1)) (zero? (car l2))) (cons 1 (lob-add (cdr l1) (cdr l2)))] [(and (one? (car l1)) (one? (car l2))) (cons 1 (lob-add1 (lob-add (cdr l1) (cdr l2))))] [else (error "lob-add: some case/11 not handled: " l1 l2)])] [else (error "lob-add: Uh-oh, some case not handled: " l1 l2)]))) ;; one? ;; a function for bits only ;; (define one? (lambda (bit) (not (zero? bit)))) ;; bit-sum-low ;; take two bits; return the low-bit of their sum ;; (define bit-sum-low (lambda (b1 b2) (cond [(and (zero? b1) (zero? b2)) 0] [(zero? b1) 1] [else 1]))) ;; bit-sum-low ;; take two bits; return the low-bit of their sum ;; (define bit-sum-low (lambda (b1 b2) (if (or (one? b1) (one? b2)) 1 0))) ;; bit-sum-low ;; take two bits; return the carry-bit of their sum ;; (define bit-sum-carry (lambda (b1 b2) (if (and (one? b1) (one? b2)) 1 0))) ; Why aren't these functions on "bit" recursive? ; Because the data def'n for "bit" isn't recursive!