“应用程序:不是过程"在计算二项式时 [英] "application: not a procedure" while computing binomial

查看:79
本文介绍了“应用程序:不是过程"在计算二项式时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在定义一个函数binomial(n k)(又称帕斯卡三角形),但出现错误:

    application: not a procedure;
    expected a procedure that can be applied to arguments
    given: 1
    arguments...:
    2

我不理解该错误,因为我认为这定义了我的功能:

    (define (binomial n k)
      (cond  ((or (= n 0) (= n k)) 1)
          (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) 

解决方案

在Scheme(通常是Lisps)中,括号位于过程应用程序的前面以及过程的最终参数之后.您已经在

中正确完成了此操作

(= n 0)
(= n k)
(- k 1)
(binomial(- n 1) (- k 1))

但是,您对binomial的一个调用中的自变量中有一个错误:

(define (binomial n k)
  (cond  ((or (= n 0) (= n k)) 1)
      (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) 
                        ***

基于上述(n)的语法,是一个应用程序,其中n应该对过程求值,并且该过程将不带任何参数地被调用.当然,这里的n实际上是 的值是一个整数,它不是过程,因此不能调用(因此称为应用程序:不是过程").您可能要删除n周围的括号:

(binomial n (- k 1))

值得一提的是,Racket博士应该突出显示与我上面相同的代码部分.当我加载代码并评估(binomial 2 1)时,会得到以下结果,其中突出显示了(n):

I am defining a function binomial(n k) (aka Pascal's triangle) but am getting an error:

    application: not a procedure;
    expected a procedure that can be applied to arguments
    given: 1
    arguments...:
    2

I don't understand the error because I thought this defined my function:

    (define (binomial n k)
      (cond  ((or (= n 0) (= n k)) 1)
          (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) 

解决方案

In Scheme (and Lisps in general), parentheses are placed before a procedure application and after the final argument to the procedure. You've done this correctly in, e.g.,

(= n 0)
(= n k)
(- k 1)
(binomial(- n 1) (- k 1))

However, you've got an error in one of your arguments to one of your calls to binomial:

(define (binomial n k)
  (cond  ((or (= n 0) (= n k)) 1)
      (else (+ (binomial(n) (- k 1))(binomial(- n 1) (- k 1)))))) 
                        ***

Based on the syntax described above (n) is an application where n should evaluate to a procedure, and that procedure will be called with no arguments. Of course, n here actually evaluates to an integer, which is not a procedure, and can't be called (hence "application: not a procedure"). You probably want to remove the parentheses around n:

(binomial n (- k 1))

It's also worth pointing out that Dr. Racket should have highlighted the same portion of code that I did above. When I load your code and evaluate (binomial 2 1), I get the following results in which (n) is highlighted:

这篇关于“应用程序:不是过程"在计算二项式时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆