Question

In: Computer Science

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 (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.

#lang racket
(provide combine)

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 :

I have tried to explain it in very simple language and
I hope that i have answered your question satisfactorily.Leave doubts in comment section if any


Related Solutions

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.
Write in Racket language. Write a non-recursive Racket function called "keep-short-norec" that takes an integer and...
Write in Racket language. Write a non-recursive Racket function called "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.
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of...
RACKET a) Write a recursive function (gen-list start end). This function will generate a list of consecutive integers, from start to end. If start > end then an empty list is generated. For example: (gen-list 1 5) ---> (1 2 3 4 5) b) write a recursive function pair-sum? that takes an integer sequence as generated by the gen-list function in exercise 4 above. This function tests whether any two adjacent values in the given list sum to the given...
How to create a divide function in (Dr Racket) programming language without using the built in...
How to create a divide function in (Dr Racket) programming language without using the built in function " / " ?
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...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED...
CS 400 Assignment 5 Recursive/Backtracking: Generating Permutations WRITE CODE IN C++ PROGRAMMING LANGUAGE WITH COMMENTS INCLUDED Description: Mimic the code for N-queen problem (https://introcs.cs.princeton.edu/java/23recursion/Queens.java.html), develop a program that generates all permutations for the set {1, 2, 3, 4, 5}. The output should contain all 5! = 120 permutations. Output Sample: P#1: 1 2 3 4 5 P#2: 1 2 3 5 4 P#3: 1 2 4 3 5 ... P#120: 5 4 3 2 1 Hint: - Thoroughly study the...
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.
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+)...
Rewrite the C PROGRAMMING LANGUAGE CODE in terms of only dereferencing (*) and pointer addition (+) AND extend the code so that allocated memory is freed properly. Thank you struct foo { int a; char b; }; int main(void) { struct foo* arr[5]; int x; for(x = 0; x < 5; x++) { arr[x] = malloc(sizeof(struct foo)); arr[x]->a = 0; arr[x]->b = 'b'; } }
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
Only Program in C for this. No other programming language is allowed. Using a function, we...
Only Program in C for this. No other programming language is allowed. Using a function, we will add a range of values of an array. The range is going to be determined by the user. In this example, if you put the following array down as: 1.5 -5.6 8.9 4.6 7.8 995.1 45.1 -5964.2 … and the user tells you to add from the 3rd element to the 6th element, your program is going to need to add the values:...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT