what does the lambda function in this solution actually do?

In def plus(y): return lambda x: x+y, how are both arguments passed to this function?

It doesn’t — or at least, plus() doesn’t pass both arguments into the lambda function. plus(3) returns lambda x: x + 3 — a function that takes in one argument and increments its argument by 3. This process is known as currying.

To address your example, three(plus(one())):

  • one() returns 1.
  • plus(one()) becomes plus(1) and returns lambda x: x + 1.
  • three(plus(one()) becomes three(lambda x: x + 1). three() calls the lambda function passed in with an argument of 3 and returns the resulting value. This gives a final result of 4.

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top