0

I'm a beginner to the Scheme language, so I'm having trouble writing a procedure to take in an n-bit number and put it into an ALU. The ALU is supposed to be constructed using 1-bit ALU's.

Here is the 1-bit ALU:

(define ALU1
  (lambda (sel a b carry-in)
    (multiplexor4 sel
                  (cons (andgate a b) 0)
                  (cons (orgate a b) 0)
                  (cons (xorgate a b) 0)
                  (multiplexor2 sub
                                (full-adder a b carry-in)
                                (full-adder a (notgate b) carry-in)))))

which, along with the multiplexors and full-adder, works.

Here is my attempt at using a couple of procedures to simulate the n-bit ALU:

(define ALU-helper
  (lambda (selection x1 x2 carry-in n)
    (if (= n 0)
        '()
        (ALU1 (selection x1 x2 carry-in)))))

(define ALUn
  (lambda (selection x1 x2 n)
    (ALU-helper (selection x1 x2 c n))))

And when it's done, it's supposed to take 2 n-bit numbers and add them, or subtract etc, according the to "selection." This would be the input:

(define x1 '(0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0   0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0) )   
(define x2 '(1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1   1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1) )
(ALUn 'add x1 x2 32)

And I get errors when running it that seem to be happening because of the "selection" parameter. I'm sure I'm just getting confused by all the parameters, but I'm not sure how to fix the problem and get the ALU to work. I'm running this using the Dr. Racket program, language R5RS.

Greg Hewgill
  • 951,095
  • 183
  • 1,149
  • 1,285
aclark
  • 219
  • 1
  • 4
  • 13
  • What errors are you getting? you should post all the relevant procedures in your question (`multiplexor4`, `multiplexor2`, `full-adder`, etc.) to make it a [SSCCE](http://homepage1.nifty.com/algafield/sscce.html). – Óscar López Apr 08 '12 at 15:01

1 Answers1

0

By putting parentheses around your arguments to ALU1 inside ALU-helper, you are asking selection to be treated as a function, and only passing 1 argument to ALU-helper. Try:

(ALU1 selection x1 x2 carry-in))))

Same thing for the call to ALU-helper in ALUn.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101