Question

(a) Explain the method of making composed functions in Scheme. Show an example of a composed function (f(g(x))). (b) Write a

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Answer:

a)

A compose function is defined between the two functions or more functions.

Here for simplicity, the compose function accepts two functions and are considered as single argument.

In a compose function, depending on the defining the functional flow, the value is evaluated.

To compose (f, g) on x, then it will be computed as, f(g(x)).

Method in Scheme:

Define the function called compose, on two functions called f and g with a value x.

Internally define a lambda function on x where

  • It first evaluates the g(x).

  • The obtained resultant from g(x) will pass as value into function f.

  • Then final result obtained from the f(g(x)) will be returned to the call function.

The value calculation is based on the functions that are passed in place of f and g.

Example:

Compute the value of acos(sin(0)).

Here sin() is function g,acos() is function f and x = 0.

Scheme:

; define the function compose on two functions

; f and g and return the value computed by f(g(x))

(define (compose f g)

(lambda (x) (f (g x))))

; function call to find the value of acos(sin(0))

; first test case

(display ((compose acos sin) 0))

(newline)

; function call to find the value of cos(sin(0))

; first test case

(display ((compose cos sin) 0))

(newline)

Sample Output:

58kFXHwes90v4n0HrZFrNnTooiD13ume1r0HrZFr

Code screen shot:

zAAAAAElFTkSuQmCC

b)

Scheme function screen shot:

Add a comment
Know the answer?
Add Answer to:
(a) Explain the method of making composed functions in Scheme. Show an example of a composed...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT