Curry-Lite: A Function That Returns an Adder

Hard ⏱ 18 min 25% acceptance ★★★★☆ 4.3
Write a function make_adder(x) that returns a new function which, when called with a single argument y, returns x + y. This is a lightweight form of currying: make_adder(5) should return a function equivalent to lambda y: 5 + y, but written as a nested def, not a lambda.

Examples

Example 1
Input
add5 = make_adder(5)
add5(3)
Output
8
Explanation

add5 closes over x=5, so calling it with 3 returns 5+3=8.

Example 2
Input
make_adder(10)(7)
Output
17
Explanation

The returned function can also be called immediately.

Constraints

  • Do not use lambda — define the inner function with a nested def statement.
  • Each call to make_adder must return an independent closure.

Topics

Functionsclosures

Companies

GoogleAirbnb

Hints

Hint 1

Define an inner function inside make_adder that references x from the enclosing scope.

Hint 2

Return the inner function itself (without calling it).

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.