“申请:不是程序"在二进制算术程序中 [英] "application: not a procedure" in binary arithmetic procedures
问题描述
我有一个简单的球拍定义,用于将二进制数相乘.它使用经过充分测试的addWithCarry"定义,该定义采用三个参数:两个列表和一个进位数字,并返回二进制和.二进制数以相反的顺序表示为列表.
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.
我用调试器单步执行了测试线,它正确地通过了递归.它在每次适当缩小 y 列表时执行 multBins,然后按预期执行 addWithCarry 函数.当它上升到堆栈时,它突然抛出一个异常应用程序:不是过程,期望一个可以应用于参数的过程",参数'(0 0 0 1 0 1 1)是最高的值x"加到总数中.我知道当您尝试将函数的结果作为带参数的函数应用时可能会发生此错误,但我在这里没有看到.看着调试器,直到最后,一切似乎都运行良好.有什么想法吗?
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))
这是 addWithCarry 定义:
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))))))))
推荐答案
在这一行中,您使用 (cons 0 x)
和 调用
,得到一些结果multBins
(rest y)r
,然后尝试调用r
:
In this line, you're calling multBins
with (cons 0 x)
and (rest y)
, and getting some result r
, and then trying to call r
:
((= (first y) 0) ((multBins (cons 0 x) (rest y))))
; ^ ^
; +--- function application -----+
同样的事情发生在下一行,你用一些参数调用 addWithCarry
,得到结果 r
,然后尝试调用 r
:
The same kind of thing is happening in the next line, where you're calling addWithCarry
with some arguments, getting a result r
, and trying to call r
:
(#t ((addWithCarry x (multBins (cons 0 x) (rest y)) 0)))))
; ^ ^
; +-------------- function application -------------+
推测其中一个返回了不适用的值 '(0 0 0 1 0 1 1)
.
Presumable the unapplicable value '(0 0 0 1 0 1 1)
is being returned by one of these.
在一个非常的简化案例中,考虑来自 DrRacket REPL 的这个成绩单:
In a very simplified case, consider this transcript from the DrRacket REPL:
> (define (value) ; a function that returns the
'(0 0 0 1 0 1 1)) ; same value that yours should
> (value) ; calling it produces the value
(0 0 0 1 0 1 1)
> ((value)) ; calling it and then calling
; return value causes the same
; error that you're seeing
; application: not a procedure;
; expected a procedure that can be applied to arguments
; given: (0 0 0 1 0 1 1)
; arguments...: [none]
您没有提到您使用的是什么编辑器/IDE/调试器,但有些应该让这更容易被发现.例如,当我加载您的代码时(减去对 test
的调用,我没有它的定义,并且定义了 first
和 rest
>),DrRacket 突出显示违规电话的位置:
You didn't mention what editor/IDE/debugger you're using, but some should have made this a bit easier to spot. For instance, when I load your code (minus the call to test
, whose definition I don't have, and with definitions of first
and rest
), DrRacket highlights the location of the offending call:
虽然我指出的两个有问题的调用都需要修复,但您现在看到的错误发生在两个调用中的第二个.
While both of the problematic calls that I've pointed out need to be fixed, the error that you're seeing right now is occurring in the second of the two.
这篇关于“申请:不是程序"在二进制算术程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!