snowparrot
snowparrot

Reputation: 297

Error in Racket/Lisp: expected a procedure that can be applied to arguments

Just a new defined arccos-function but I don't find the error:

(define (arccos z)
  (atan (
         (/
         (sqrt (-
               1 (expt (cos z) 2)))
         (cos z)))))

May you help me? Error message:

expected a procedure that can be applied to arguments
  given: 1.1447765772467506
  arguments...: [none]
for (arccos 1)

Upvotes: 1

Views: 1608

Answers (2)

soegaard
soegaard

Reputation: 31145

The error type is a common one - so here is how to quickly spot where the error is in a program.

Run it in DrRacket. Notice that this expression is colored red:

       (
         (/
          (sqrt (-
                 1 (expt (cos z) 2)))
          (cos z)))

The error message says: "expected a procedure that can be applied to arguments given". The last part implies that Racket expected ( ... ) to be an application of a procedure (function). However the first argument is : (/ ...) and the result of a division is a number.

That is: When you get this error always look at the first expression.

Here the problem is an extra layer of parentheses ( (/ ...) ) should be (/ ...). In other cases use display to print out the result of the first expression in order to see what went wrong.

Note: In can be helpful to use the following indentation convention when dealing with arithmetical operations:

(operation argument1
           argument2
           ...)

In this example:

  (atan (/ (sqrt (- 1
                    (expt (cos z) 2)))
           (cos z))))

Upvotes: 2

Óscar López
Óscar López

Reputation: 236122

Try this:

(define (arccos z)
  (atan (/ (sqrt (- 1 (expt (cos z) 2)))
           (cos z))))

There was an unnecessary pair of brackets after atan, also notice that correctly indenting will make this type of errors easier to spot.

Upvotes: 1

Related Questions