hotinlavacoolinjava
hotinlavacoolinjava

Reputation: 45

What does the following function do in Scheme programming language

(define (unknown (lambda (x y)
  (cond
    ((null? y) y)
    ((x (car y)) (unknown x (cdr y))))
    (else (cons (car y) (unknown x (cdr y)))))))

I'm a newbie when it comes to scheme and wanted to know the purpose of this function which I came across in a textbook. My main doubt lies as to what ((x (car y)) does. How does this expression get executed without any operators and yet I don't come across any errors while compiling. Although I'm unable to run the program because the values I input for x are apparently not applicable for the function. Please help.

Upvotes: 2

Views: 723

Answers (1)

Nathan Hughes
Nathan Hughes

Reputation: 96385

Scheme functions can take functions as arguments, and can return functions. Your code makes sense if you pass in a function as an argument.

If you call the code like this:

(unknown even? '(1 2 3 4 5))

then it should return the list (1 3 5). This is a filtering function that returns members of y where the result of applying the function x to the member is false.

Upvotes: 3

Related Questions