Take the 2-minute tour ×

I need to take a number from a list and convert it to a number so that i can pass it as a parameter.

im trying to make a 1-bit adder in scheme. i've written the code for the or gate and the xor gate and also the half adder and now im trying to combine them all to make a full adder. im not sure if im going about it the right way. any input will be appreciated thank you.

(define or-gate
 (lambda (a b)
  (if (= a 1)
    (if (= b 1)
(define xor-gate
 (lambda (a b)
  (if (= a b)

(define ha
 (lambda (a b)
  (list (xor-gate a b)(and-gate a b))))

(define fa
 (lambda (a b cin)
  (or-gate (cdr(ha cin (car (ha a b))))(cdr(ha a b)))))

the issue i get when i run the program is that the half adder (ha) function outputs a list as a value and that makes the values incompatible with my other procedures because they require numbers and not lists. i feel like there is a simple solution but i dont know it.

share|improve this question

2 Answers 2

It would be a lot easier reading your code if you had a contract; it took me quite a while to deduce that your half-adder was returning a list of bits.

Next, it looks to me like you're having a simple issue with "cdr". The "cdr" operator does not return the second element of a list, but the "rest" of the list. Check out the difference between (cdr (list 1 1)) and 1. The first produces a list containing 1, the second one produces the number 1.

share|improve this answer
i'm not exactly sure what you mean. the simple logic gates you can just pass in a 1 or 0 and for the half adder the you do the same but the output is supposed to be a list. this is a programming assignment for a class –  wyatt Apr 6 '12 at 6:58
Yeah, you're right! I misread your code. Let me revise my comment (somewhat). Hang on... –  John Clements Apr 6 '12 at 6:59
i changed all the conditions from (if (= a 1)) etc. to the equals? after doing this i now get an output but it is always zero no matter what the input it –  wyatt Apr 6 '12 at 7:16
Nope, this isn't what you want to do. Stick with =. The "equals?" is just covering up your real problem: the list containing 1 is not the same value as 1. You need a different way to refer to the second element of the list. –  John Clements Apr 6 '12 at 7:29
i used cdr because the list only contains 2 elements –  wyatt Apr 6 '12 at 7:30

For avoiding confusion, it's a good idea to use the procedures first and second instead of car and cadr for accessing the first and second elements of a list, respectively. Notice that cdr does not return the second element of a list, instead it returns the rest of a list, which is another list.

Anyway, the fa procedure in the question doesn't look right to me. It should be returning a list with both the sum and cout values. Here's one possible way to implement it, using first and second as suggested:

(define fa
  (lambda (a b cin)
    (list (first (ha a (first (ha b cin))))
          (or-gate (second (ha a (first (ha b cin))))
                   (second (ha b cin))))))

... But that looks confusing. Notice that we're calculating (ha b cin) in three parts, a better idea would be to save the repeated calculation in a variable, like this:

(define fa
  (lambda (a b cin)
    (let* ((partial1 (ha b cin))
           (partial2 (ha a (first partial1))))
      (list (first partial2)
            (or-gate (second partial2) (second partial1))))))

As a final note, in the book SICP there's a very nice implementation of adders as part of a simulator for digital circuits, take a look at it for further ideas on how to improve your code.

share|improve this answer

Your Answer


