0

Write a Racket function named compare that takes two arguments, functions f and g, each of which takes a single integer argument.

compare returns a function of one argument, an integer n. The returned function computes the value f(n) > g(n). For example:

>(define double (lambda (n) (* n 2))) 
>(define square (lambda (n) (* n n)))
>(define 2n+1 (lambda (n) (add 1 (double n)))

>((compare square double) 2)    ; is (2*2) > (2*2)?
#f
>((compare square double) 5)    ; is (5*5) > (5*2)?
#t

Here is what I have so far:

(define compare
  (lambda (f g)
    (lambda (int)
      (lambda (int-two)
          (>= (f g))))))

2 Answers 2

2

Write a Racket function named compare...

(define compare ...

...that takes two arguments, functions f and g, each of which takes a single integer argument.

(define compare
  (lambda (f g)
    ...

...compare returns a function of one argument, an integer n.

(define compare
  (lambda (f g)
    (lambda (n)
      ...

...The returned function computes the value f(n) > g(n).

(define compare
  (lambda (f g)
    (lambda (n)
      (> (f n) (g n)))))
Sign up to request clarification or add additional context in comments.

1 Comment

alternatively (and more succinctly), (define (compare f g) ...)
0

I think you need to call your functions f or g at some points.

You also need to provide the function double in f and square in g in order to met your condition >=.

You can try the following code in the Racket interpreter:

(define double (lambda (n) (* n 2))) 
(define square (lambda (n) (* n n)))

(define compare
  (lambda (f g)
    (lambda (int)
       (printf "F:~A G:~A Condition:~A~%" (f int) (g int) (>= (f int) (g int)))
       (>= (f int) (g int)))))

(let ((compare-function (compare double square)))
  (printf "Result 1: ~A~%" (compare-function 2))
  (printf "Result 2: ~A~%" (compare-function 5)))

Which should provide the following results:

F:4 G:4 Condition:#t
Result 1: #t
F:10 G:25 Condition:#f
Result 2: #f

Comments

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.