1
votes

I'm having a hard time understanding how to build a solution to a homework problem I have for lambda expressions. I have to write a function that takes a single argument, F, that is a predicate function and returns a new function that is F's converse.

I know that somewhere in my function I will return the not of the value from the passed in predicate function to return the converse but I'm confused about the rest of the problem description. The problem states that "You will need a lambda inside a lambda. Since you don't know how many arguments F will take (and might in fact take a variable number) you will have to use apply and the syntax for defining a lambda expression that takes any number of arguments"

I don't understand how to set up the nested lambda expressions to do what I want with returning the converse of whatever F may be. I've been experimenting with a few different things just to see if I can get anywhere, but I don't understand how nested lambda expressions work enough to get me anywhere.

(define converse
  (lambda (F) 
    (lambda
      (apply (not (F))))))

I know this won't work but I need help understanding how to set up my nested lambda expressions to do what I want.

1

1 Answers

6
votes

You're very close to the answer:

(define converse
  (lambda (f)
    (lambda args
      (not (apply f args)))))

Basically, you were missing the args argument for the innermost lambda, which will hold the variable number of arguments that f can receive. And the not is to be applied after calling f on the arguments. For example, take this predicate with two arguments:

(define (test x y)
  (> x y))

See how it normally works:

(test 10 5)
=> #t
(test 5 10)
=> #f
(test 10 10)
=> #f

And now see how it works after converse has been applied to test. Notice that it had no problem dealing with two arguments:

((converse test) 10 5)
=> #f
((converse test) 5 10)
=> #t
((converse test) 10 10)
=> #t

As a side note: in Racket, the converse procedure we just implemented already exists, and it's called negate. We could implement converse as easily as this:

(define converse negate)