Code Explanation:
Line 1–2: Defining combine
python
Copy
Edit
def combine(f, g):
return lambda x: f(g(x))
This function takes two functions, f and g, as arguments.
It returns a new function that takes a value x, applies g(x), and then applies f(...) to the result.
This is called function composition: f(g(x)).
Line 4: Lambda f
python
Copy
Edit
f = lambda x: x * 2
Defines a lambda function that doubles its input.
Line 5: Lambda g
python
Copy
Edit
g = lambda x: x + 3
Defines a lambda function that adds 3 to its input.
Line 6: Combining f and g
h = combine(f, g)
This creates a new function h where:
h(x) = f(g(x)) = (x + 3) * 2
Line 7: Print Result
print(h(4))
Let’s evaluate this:
g(4) → 4 + 3 = 7
f(7) → 7 * 2 = 14
So h(4) → 14
Final Output:
14
.png)

0 Comments:
Post a Comment