简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM