I have a simple Racket definition for multiplying binary numbers together. It uses a well-tested "addWithCarry" definition that takes three parameters: two lists and a carry digit and returns the binary sum. The binary numbers are represented as lists in reverse order.
I stepped through the test line with the debugger, and it goes through the recursion properly. It performs the multBins each time shrinking the y list as appropriate, then conducts the addWithCarry functions as expected. As it rises back up the stack, it suddenly throws an exception "application: not a procedure, expected a procedure that can be applied to arguments" with the parameter '(0 0 0 1 0 1 1) which is the value of the highest "x" added to the total. I know this error can occur when you are attempting to apply the result of a function as a function with a parameter, but I don't see this here. Watching the debugger, everything seems to be working perfectly until the very end. Any ideas?
(define (multBins x y)
(cond
((null? y) '() )
((= (first y) 0) ((multBins (cons 0 x) (rest y))))
(#t ((addWithCarry x (multBins (cons 0 x) (rest y)) 0)))))
(test (multBins '(1 0 1 1)'(1 1 0 1))'(1 1 1 1 0 0 0 1))
Here is the addWithCarry definition:
(define (addWithCarry x y carry)
(cond
((and (null? x)(null? y)) (if (= carry 0) '() '(1)))
((null? x) (addWithCarry '(0) y carry))
((null? y) (addWithCarry x '(0) carry))
( #t (let ((bit1 (first x))
(bit2 (first y)))
(cond
((= (+ bit1 bit2 carry) 0) (cons 0 (addWithCarry (rest x) (rest y) 0)))
((= (+ bit1 bit2 carry) 1) (cons 1 (addWithCarry (rest x) (rest y) 0)))
((= (+ bit1 bit2 carry) 2) (cons 0 (addWithCarry (rest x) (rest y) 1)))
( #t (cons 1 (addWithCarry (rest x) (rest y) 1))))))))