为了账号安全,请及时绑定邮箱和手机立即绑定

二进制算术过程中的“应用程序:不是过程”

二进制算术过程中的“应用程序:不是过程”

ABOUTYOU 2019-10-12 13:41:02
我有一个简单的球拍定义,用于将二进制数相乘。它使用经过良好测试的“ addWithCarry”定义,该定义带有三个参数:两个列表和一个进位数字,并返回二进制和。二进制数以相反的顺序表示为列表。我使用调试器逐步完成了测试行,并且正确地进行了递归。每次适当缩小y列表时,它都会执行multBins,然后按预期进行addWithCarry函数。当它返回堆栈时,突然抛出异常“应用程序:不是过程,应为可以应用于自变量的过程”,其参数为'(0 0 0 1 0 1 1),这是最高值“ x”添加到总数中。我知道当您尝试将函数结果应用为带有参数的函数时,会发生此错误,但是我在这里看不到此错误。看着调试器,直到最后一切似乎都运转良好。有任何想法吗?(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定义:(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))))))))
查看完整描述

1 回答

?
PIPIONE

TA贡献1829条经验 获得超9个赞

在这一行中,您multBins使用(cons 0 x)和进行调用(rest y),并得到一些结果r,然后尝试调用r:


((= (first y) 0) ((multBins (cons 0 x) (rest y))))

;                ^                              ^

;                +--- function application -----+

在下一行中,您正在addWithCarry使用一些参数进行调用,获取结果r并尝试调用r:


(#t              ((addWithCarry x (multBins (cons 0 x) (rest y)) 0)))))

;                ^                                                 ^

;                +-------------- function application -------------+

可能'(0 0 0 1 0 1 1)是其中之一返回了不适用的值。


在非常简化的情况下,请考虑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凸显违规调用的位置:

//img1.sycdn.imooc.com//5da1679000012f3b09580763.jpg

虽然我已经指出了两个有问题的调用,但都需要修复,但是您现在看到的错误是在这两个中的第二个发生。


查看完整回答
反对 回复 2019-10-12
  • 1 回答
  • 0 关注
  • 341 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信