collimarco
collimarco

Reputation: 35400

Scheme: The object () is not applicable

It's a beginner question. However it's more than 2 hours that I'm trying to find out the error (I've also made searches) but without success.

(define a (lambda (l i) (
    (cond ((null? l) l)
          (else (cons (cons (car l) i) (a (cdr l) i))))
)))

The function a should pair the atom i with each item of l. For example:

(a '(1 2 3) 4) should return ((1 4) (2 4) (3 4))

However when I try to use invoke the function I get:

The object () is not applicable

What's the error in my function?

I am using mit-scheme --load a.lisp to load the file. Then I invoke the function a by typing in interactive mode.

Upvotes: 2

Views: 1014

Answers (1)

Renzo
Renzo

Reputation: 27424

The error, as usually happens in lisp languages, depends on the wrong use of the parentheses, in this case the extra parentheses that enclose the body of the function.

Remove it and the function should work:

(define a (lambda (l i) 
            (cond ((null? l) l)
                  (else (cons (cons (car l) i) (a (cdr l) i))))))

Rember that in lisp parentheses are not a way of enclosing expressions, but are an important part of the syntax: ((+ 2 3)) is completely different from (+ 2 3). The latter expression means: sum the value of the numbers 2 and 3 and return the result. The former means: sum the value of the numbers 2 and 3, get the result (the number 5), and call it as a function with zero parameters. This obviously will cause an error since 5 is not a function...

Upvotes: 5

Related Questions