0

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)))))) 
Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Mary Martinez
  • 314
  • 5
  • 12
  • I should mention I'm using #lang scheme with Dr.Racket – Mary Martinez Oct 21 '13 at 06:54
  • I tried calling binomial(1 2) – Mary Martinez Oct 21 '13 at 06:55
  • 1
    what is `(binomial (n) (- k 1))` for a syntax? Try to remember the syntax for a variable in Scheme and compare with the use of `(n)`. – Rainer Joswig Oct 21 '13 at 07:03
  • note that this algorithm is extraordinarily inefficient - _exponential_ number of recursive calls! – sds Oct 21 '13 at 11:36
  • Please search for specific error messages on Stack Overflow _before_ posting the question. (If you did, then please forgive me this comment.) There are [other question about this error](http://stackoverflow.com/search?q=%22application%3A+not+a+procedure%22+is%3Aquestion). – Joshua Taylor Oct 21 '13 at 12:49
  • possible duplicate of [“application: not a procedure” in binary arithmetic procedures](http://stackoverflow.com/questions/19022704/application-not-a-procedure-in-binary-arithmetic-procedures) – Joshua Taylor Oct 21 '13 at 12:52
  • oh, actually, it's more an error of [“application: not a procedure” in determinant code](http://stackoverflow.com/q/18470443/1281433) where someone else tried to call a mathematical function by wrapping its argument list in parenthesis, which is _not_ the case with the question I already mentioned as a possible duplicate. – Joshua Taylor Oct 21 '13 at 13:01
  • Btw, nowadays you should use `#lang racket` instead of `#lang scheme` – Metaxal Oct 21 '13 at 15:41

3 Answers3

2

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:

error in dr racket

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
0

Your error is here:

binomial(n)

n is an integer, not a function. If you put parentheses around it like that, scheme tries to invoke an integer as a function, which naturally produces an error.

Ben
  • 683
  • 3
  • 5
0

This is the correct code:

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

Problem is at here:

(binomial (n) (- k 1))
Hari Chaudhary
  • 630
  • 1
  • 7
  • 20