Apex Predator
Apex Predator

Reputation: 171

How to call a function in scheme

I was trying to test a small portion of code and for some reason I have some errors. here is the code. here tab is just a function returning a list and translate is another function.

(define p
  (let ((x (car tab)) (y (cadr tab)))
    (list translate(x) y)))

Upvotes: 0

Views: 8570

Answers (2)

Sylwester
Sylwester

Reputation: 48775

+ is a common procedure in Scheme and if you evaluate it will evaluate the symbol and you'll get a implementation dependent representation of the procedure object:

+ ; ==> <procedure: +> (or something similar)

Now + is just a variable that, when evaluated, evaluates to a procedure. How to call it is just to suround it with parentheses:

(+) ; ==> 0

What happens is that Scheme sees the parentheses, then evaluates the first argument + and it becomes the procedure <procedure: +>. Since it'a procedure the arguments are evaluated in any order and last the procedure is applied with those evaluated arguments.

If tab is a procedure object you cannot apply car or cdr to it. You can do it to the result of calling it if it evaluates to a pair. Likewise if you want to call a procedure translate with argumentx it needs to look like (translate x). Putting it all together:

(define p
  (let* ((tab-result (tab))
         (x (car tab-result)) 
         (y (cadr tab-result)))
    (list (translate x) y)))

Upvotes: 1

sepp2k
sepp2k

Reputation: 370435

A function call is written as (f args) where f is the name of the function and args a space-separated sequence of arguments.

So to call tab without arguments, you'd write (tab) and to call translate with the argument x, you'd write (translate x).

Upvotes: 2

Related Questions