1

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 Answer 1

6

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)
Sign up to request clarification or add additional context in comments.

7 Comments

You're probably using a teaching language that doesn't allow for this: lambda args . That normally works in Scheme, and it's the correct way to define a lambda with a variable number of arguments, but your teaching language is marking it as an error. Try using a more advanced teaching language
So when I test that I get an error wanting parens around the "args" in the second lambda. When I make the change and test again I get another error saying that my procedure expects only 1 argument, but found 2.
No, you must not add parenthesis around args! that's the way we tell Scheme that a variable number of arguments is expected. Tell me, what teaching language are you using?
I think it was just to do with my current version of DrRacket, I get an error there, but when I test on the Linux servers version it seems to work fine. I have DrRacket localy with version 5.3.5 but it works on the Linux servers version of Racket 5.1.3
The problem description is clear on this "the syntax for defining a lambda expression that takes any number of arguments". That's precisely this: (lambda args <body>) , where the parameters go naked, without parenthesis.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.