0

Ok so we just started learning scheme in class and our first assignment was given. Now I am new to scheme so I am still unsure if what I did is correct or not, but so far I am having a problem with my code for one of the homework questions.

Question 3:

3.1 Define a global name PI and associate the value 3.14159265 to it. [3]

3.2 Define a global Scheme procedure (areac d) that has the same function as defined in the C-program. [3]

3.3 Define a global Scheme procedure (volumec d h) [3]

3.4 Define a global Scheme procedure (TotalVolume) ; no parameter [3]

3.5 Define a global Scheme procedure (main) ; no parameter

This is sample C code that was given for us to go off of

#include <stdio.h>
const double PI = 3.14159265;
double areac(double d) {
    double a;
    a =PI*(d/2)*(d/2);
    return a;
}
double volumec(double d, double h) {
    double a, v;
    a = areac(d);   // call areac() here
    v = a*h/3;
    return v;
}
double TotalVolume() {
    double v1, v2, v3, v4, v5;
    v1 = volumec(1, 1);
    v2 = volumec(2, 2);
    v3 = volumec(3, 3);
    v4 = volumec(4, 4);
    v5 = volumec(5, 5);
    return v1+v2+v3+v4+v5;
}
void main() {
    TotalVolume();

And this is what I have for scheme:

(define PI 3.14159265)
(define (areac d)
  (let* 
      (
       (a (* (* PI (/ d 2)) (/ d 2)))
       )
    (display a)))
 (define (volumec d h)
   (let
       (
       (a (areac(d))))
    (let ((v (/ (* a h) 3)))
     (display v) ))
     )
 (define (TotalVolume)
   (let*
       (
        (v1 (volumec(1 1)))
        (v2 (volumec(2 2)))
        (v3 (volumec(3 3)))
        (v4 (volumec(4 4)))
        (v5 (volumec(5 5)))
        )
   (display (+ (+ (+ (+ v1 v2) v3) v4) v5))))
 (define main
   (TotalVolume))

However I get an error when I run my program:

application: not a procedure;

expected a procedure that can be applied to arguments

given: 1

arguments...:

I get an error on the TotalVolume procedure and it points from (volumec(1 1)) to (let*

If anyone could help me out with what I am doing wrong that would be appreciated

Thanks!

Rakeen Huq
  • 37
  • 1
  • 5
  • possible duplicate of ["application: not a procedure" in binary arithmetic procedures](http://stackoverflow.com/questions/19022704/application-not-a-procedure-in-binary-arithmetic-procedures). As Barmar points out, in places you have `(proc (arg1 arg2))` instead of `(proc arg1 arg2)`. The former tries to evaluate `(arg1 arg2)`, which means evaluating arg1 and arg2, taking the value of arg1 and *applying* it as a procedure with the value of arg2 as an argument. Of course, the value of arg1 is not a procedure, hence the message "application: not a procedure". – Joshua Taylor Apr 13 '15 at 15:34

1 Answers1

2

Several problems:

  1. Your procedures don't return values, they just display the values they computed.

  2. You didn't define main as a procedure, since you didn't put parenthese around the name or define it using a lambda expression.

  3. Arguments to a procedure are not put in an extra level of parenthese. The syntax is (proc arg1 arg2), not (proc (arg1 arg2)). You got this right when calling built-in procedures like / and *, but you did it wrong when calling your own procedures.

(define PI 3.14159265)
(define (areac d)
  (* (* PI (/ d 2)) (/ d 2))))
(define (volumec d h)
  (let ((a (areac d)))
    (/ (* a h) 3)))
(define (TotalVolume)
  (let ((v1 (volumec 1 1))
        (v2 (volumec 2 2))
        (v3 (volumec 3 3))
        (v4 (volumec 4 4))
        (v5 (volumec 5 5)))
    (+ v1 v2 v3 v4 v5)))
 (define (main)
   (TotalVolume))
Barmar
  • 741,623
  • 53
  • 500
  • 612