方案:不是程序(Racket 博士)

Scheme: Not a procedure (Dr. Racket)

我 运行 这个程序在 Dr. Racket 中使用 R5RS 方案并且在行 (+ 1 IntDivide((- x y) y)):

上收到这个错误

"application: not a procedure; expected a procedure that can be applied to arguments given: 5 arguments...:"

该程序应该return 使用减法计算两个整数除法的商。由于这是一道作业题,我不会问我的解决方案是否正确(我可以稍后调试),而是问是什么导致了这个错误。这似乎通常是由过多的括号引起的,但我似乎找不到它们。任何帮助将不胜感激。

(define IntDivide (lambda (x y)
  (if (eqv? (integer? x) (integer? y))
    (begin
      (if (= y 0)
        (begin
          (write "Can't divide by zero") (newline)
          -1
        )
      )

      (if (= (- x y) 0)
        1
      )

      (if (< x y)
        0
      )

      (if (> x y)
        (+ 1 IntDivide((- x y) y))
      )
    )
  )
  (write "Please only input integers")
))

提前致谢!

像调用任何其他函数一样调用 IntDivide

(+ 1 (IntDivide (- x y) y))

除了将运算符移到括号内之外,您还需要将 if 替换为 cond:

(define IntDivide
  (lambda (x y)
    (if (eqv? (integer? x) (integer? y))
      (cond ((= y 0) (write "Can't divide by zero")
                     (newline)
                     -1)
            ((= x y) 1)
            ((< x y) 0)
            ((> x y) (+ 1 (IntDivide (- x y) y))))
      (write "Please only input integers"))))

您现在使用内部 if 表达式的方式将不起作用,因为它们不会自动 return。他们只是评估,然后结果被丢弃。