Home > front end >  Need explanation with output of a python list with lambda function
Need explanation with output of a python list with lambda function

Time:06-21

The output of the program

m = [1, 2, 3, 4, 5]
d = lambda y: (d(y[1:])   y[:1] if y else [])
print(d(m))

is [5, 4, 3, 2, 1].

I can understand print(d(m)) is taking list m as a parameter and in the lambda function y = m. Then, y[1:] = [2, 3, 4, 5] and y[:1] = [1]. But what is happening after that? Can anyone explain how this output is coming?

CodePudding user response:

The function is reversing the list taked as input with recursion.

The algorithm works like this:

It takes the first item of the list and it takes it to the end. Then, the part that is left, will do the same thing over and over until it gets an empty list.

CodePudding user response:

Lambda d is a pure function and so we can evaluate the expression using substitution. To evaluate d(m) we first start by substituting d and m for their values -

d(m)
# d := lambda y: d(y[1:])   y[:1] if y else []

(lambda y: d(y[1:])   y[:1] if y else [])(m)
# m := [1,2,3,4,5]

Then we apply the lambda to its argument -

(lambda y: d(y[1:])   y[:1] if y else [])([1,2,3,4,5]) 
# y := [1,2,3,4,5]

d([1,2,3,4,5][1:])   [1,2,3,4,5][:1] if [1,2,3,4,5] else []
d([1,2,3,4,5][1:])   [1,2,3,4,5][:1]
d([2,3,4,5])   [1]
# d := lambda y: d(y[1:])   y[:1] if y else []

Using Python's eager evaluation strategy, we evaluate all arguments before applying functions. Continue this strategy until the answer is reached -

(lambda y: d(y[1:])   y[:1] if y else [])([2,3,4,5])   [1]
# y := [2,3,4,5]

(d([2,3,4,5][1:])   [2,3,4,5][:1] if [2,3,4,5] else [])   [1]
(d([2,3,4,5][1:])   [2,3,4,5][:1])   [1]
(d([3,4,5])   [2])   [1]
d([3,4,5])   [2]   [1]
# d := lambda y: d(y[1:])   y[:1] if y else []
(lambda y: d(y[1:])   y[:1] if y else [])([3,4,5])   [2]   [1]
# y := [3,4,5]

(d([3,4,5][1:])   [3,4,5][:1] if [3,4,5] else [])   [2]   [1]
(d([3,4,5][1:])   [3,4,5][:1])   [2]   [1]
(d([4,5])   [3])   [2]   [1]
d([4,5])   [3]   [2]   [1]
# d := lambda y: d(y[1:])   y[:1] if y else []
(lambda y: d(y[1:])   y[:1] if y else [])([4,5])   [3]   [2]   [1]
# y := [4,5]

(d([4,5][1:])   [4,5][:1] if [4,5] else [])   [3]   [2]   [1]
(d([4,5][1:])   [4,5][:1])   [3]   [2]   [1]
(d([5])   [4])   [3]   [2]   [1]
d([5])   [4]   [3]   [2]   [1]
# d := lambda y: d(y[1:])   y[:1] if y else []
(lambda y: d(y[1:])   y[:1] if y else [])([5])   [4]   [3]   [2]   [1]
# y := [5]

(d([5][1:])   [5][:1] if [5] else [])   [4]   [3]   [2]   [1]
(d([5][1:])   [5][:1])   [4]   [3]   [2]   [1]
(d([])   [5])   [4]   [3]   [2]   [1]
d([])   [5]   [4]   [3]   [2]   [1]
# d := lambda y: d(y[1:])   y[:1] if y else []
(lambda y: d(y[1:])   y[:1] if y else [])([])   [5]   [4]   [3]   [2]   [1]
# y := []

(d([][1:])   [][:1] if [] else [])   [5]   [4]   [3]   [2]   [1]
([])   [5]   [4]   [3]   [2]   [1]

At this point the base case is reached and d no longer expands to an expression with another d. We can start to collapse the stack for the final output -

[]   [5]   [4]   [3]   [2]   [1]
[5]   [4]   [3]   [2]   [1]
[5,4]   [3]   [2]   [1]
[5,4,3]   [2]   [1]
[5,4,3,2]   [1]
[5,4,3,2,1]
  • Related