Question

In: Computer Science

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.

Solutions

Expert Solution

Answer:

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:

//please upvote//


Related Solutions

Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a...
Write in Racket Language Write a recursive Racket function "all-same" that takes a string as a parameter and evaluates to true iff every character in the string is the same. Note: A string of length 0 or 1 should also evaluate to true.
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY Write a Racket function "combine" that...
PLEASE GIVE THE CODE IN RACKET PROGRAMMING RECURSIVE LANGUAGE ONLY 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...
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a Python function that takes two parameters: the first a list of strings and the...
Write a Python function that takes two parameters: the first a list of strings and the second a single string. The function should return True or False depending on whether the string is in the list or not. For example, if the list contains eggs, milk, bananas, and the second parameter is pumpkin, the function should return False. Thank you.
Write a function in c++, called afterAll that takes two parameters, a vector of string and...
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the...
Write a function named "check_matrix" which takes two matrices as parameters and returns 1 if the matrices are same or 0 otherwise. Set appropriate parameters and return type if necessary.
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the...
Python Problem 3 Write a function named enterNewPassword. This function takes no parameters. It prompts the user to enter a password until the entered password has 8-15 characters, including at least one digit. Tell the user whenever a password fails one or both of these tests.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT