Question

Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates...

Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result.

For example (combine add1 sub1) should evaluate to a function equivalent to (define (h x) (sub1 (add1 x))). You will need to use a lambda function to achieve your result. Essentially you want (combine f g) to evealuate to a lambda function with f and g embedded inside.

You can test your combine with invications such as ((combine add1 add1) 1), which should evaluate to 3 since the anonymous function returned by combine is being applied to 1.

Note: This is an example of a "function closure". f and g are defined in the scope of combine, but the lambda embeds a copy of them in the lambda function that can be used outside of combine's scope. When creating a lambda function in Racket, anything in the enclosing scope can be captured in this way so that the lambda is functional outside of its defining scope.

Homework Answers

Answer #1

racket code:

; combibe function that returns a function
; which is composition of two input functions
(define (combine f g)
; returning lambda function which is f applied first and g applied to f's result
(lambda (x) (g (f x)))
)


; Testing combine function for different functions

(define (square x) (* x x))

(writeln ((combine add1 add1) 1))
(writeln ((combine sub1 add1) 1))
; takes square first and then add1
(writeln ((combine square add1) 2))

Testing output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates...
Write a Racket function "combine" that takes two functions, f and g, as parameters and evaluates to a new function. Both f and g will be functions that take one parameter and evaluate to some result. The returned function should be the composition of the two functions with f applied first and g applied to f's result. For example (combine add1 sub1) should evaluate to a function equivalent to (define (h x) (sub1 (add1 x))). You will need to use...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters,...
Write in Racket Language Write a recursive Racket function "sum" that takes two integers as parameters, each greater or equal to zero, and evaluates to their sum. In this problem, you must use the built-in functions "add1" and "sub1" and may not use the built-in functions "+" or "-". For example, (sum 2 3) should evaluate to 5. Note: (add1 5) evaluates to 6 and (sub1 4) evaluates to 3. Hint: like you saw in the "append" lecture, treat one...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes...
***************PLEASE GIVE ANSWERS IN RACKET PROGRAMMING LANGUAGE ONLY****************** Write a recursive Racket function "update-if" that takes two functions, f and g, and a list xs as parameters and evaluates to a list. f will be a function that takes one parameter and evaluates true or false. g will be a function that takes one parameter and evaluates to some output. The result of update-if should be a list of items such that if x is in xs and (f x)...
Write a Racket function named listlen that takes a list as a parameter and evaluates to...
Write a Racket function named listlen that takes a list as a parameter and evaluates to the number of elements in the list. For example (listlen empty) should evaluate to 0 and (listlen '(1 2 3)) should eveluate to 3.
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list...
-RACKET LANGUAGE ONLY- Write a non-recursive Racket function "keep-short-norec" that takes an integer and a list of strings as parameters and evaluates to a list of strings. The resulting list should be all strings on the original list, maintaining their relative order, whose string length is less than the integer parameter. For example, (keep-short-rec 3 '("abc" "ab" "a")) should evaluate to '("ab" "a") because these are the only strings shorter than 3. Your solution must not be recursive. You will...
Please write function in Racket language. Write a recursive Racket function "sum-diff" that takes two lists...
Please write function in Racket language. Write a recursive Racket function "sum-diff" that takes two lists of integers that are the same length and evaluates to an integer. The resulting integer should be the sum of the absolute value of the differences between each pair of integers with the same index in the two lists. For example (sum-diff '(-1+2+3)'(123)) should evaluate to 12 because the absolute value of the differences between (-1 and 1), (-2 and 2) and (-3 and...
How do you compose two functions, f and g, when the image of the first function...
How do you compose two functions, f and g, when the image of the first function is not a subset of the domain of the second function? Example: Let g be the map from the Riemann sphere to the complex numbers (where g((0,0,1))=infinity) and f be the conjugation map from the complex numbers to the complex numbers. How can one compute f(g((0,0,1))) when infinity is not a complex number?
Consider the following functions f(x) =x^2, g(x) = lnx, h(x) = cosx For each of the...
Consider the following functions f(x) =x^2, g(x) = lnx, h(x) = cosx For each of the following parts, you may use compositions, products, and sums of thefunctions above, but no others. For example, we can combine in the following waysh(g(x)) = cos(lnx), or g(x)h(x) = lnxcosx, or g(x) +h(x) = lnx+ cosx show how derivative rules apply to the function you came up within order to produce the requested derivative. 1)A functionk(x) whose derivative is k′(x) = −tanx= -(sinx/cosx) 2)...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT