xManternach14
xManternach14

Reputation: 1

Racket Programing - creating a function that takes two arguments and returns a function

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))))))

Upvotes: 0

Views: 451

Answers (2)

Robert
Robert

Reputation: 2812

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

Upvotes: 0

Renzo
Renzo

Reputation: 27444

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)))))

Upvotes: 2

Related Questions