0

When writing my code in Scheme, the error of

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

appears. I am supposed to use Heron's formula to calculate the formula of the area of a triangle.

Here is my code:

(define (s a b c)
  (/ (+ a b c) 2))

(define (area a b c)
  ((sqrt (*(- (s a b c)(a))
           (-(s a b c)(b))
           (-(s a b c)(c)))
         )))

Again the output is:

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

Will Ness
  • 70,110
  • 9
  • 98
  • 181

2 Answers2

1

You have too many parentheses:

(define (s a b c)
  (/ (+ a b c) 2))

(define (area a b c)
  (sqrt (* (- (s a b c) a)
           (- (s a b c) b)
           (- (s a b c) c))))

In languages like C, you can always add more parentheses. In Scheme, extra parentheses are always an error. I would probably write the function like this:

(define (area a b c)
  (let ((s (/ (+ a b c) 2)))
    (sqrt (* s (- s a) (- s b) (- s c)))))

Here s is calculated only once, and there is no need for an auxiliary procedure. Here's an example:

> (area 3 4 5)
6

You can run the program at https://ideone.com/Co94oe. By the way, it is correct form to stack up the parentheses at the end of the line, instead of moving them to a line of their own.

user448810
  • 17,381
  • 4
  • 34
  • 59
  • Heron of Alexandria figured out the [correct formula](https://en.wikipedia.org/wiki/Heron%27s_formula) about 2000 years ago. You are missing a multiplication by _s_. And you should calculate `(s a b c)` only once, then store the result in a temporary variable. – user448810 Sep 07 '18 at 19:59
0

In addition to user44810's entirely correct answer, let me encourage you to consider using DrRacket's stepper in order to identify the error. In this case, you'll see the step just before the error shows that you're about to call the result as a procedure.

John Clements
  • 16,895
  • 3
  • 37
  • 52