Home > Blockchain >  got this as a question in my university exams(python), would really appreciate if someone could expl
got this as a question in my university exams(python), would really appreciate if someone could expl

Time:11-26

What is the output of the following?

(lambda x:x((lambda x:x(lambda x:x))(x(x))))(lambda x:x)(lambda x:x x)(3)

Running it gives 6. I have some understanding of how lambda expressions work, but this is a tad bit too extreme for me.

CodePudding user response:

Here is the website where you can visualize your execution step by step: Python Tutor: Visualize code

CodePudding user response:

That's just annoying on an exam. But anyway the first 4 lambdas are all saying for a given input (x) return that input (x). Then your last one says return x x (or 2x). So for any given input you'll get back of it. But to work through:

'''

(lambda x:x((lambda x:x(lambda x:x))(x(x))))(lambda x:x)(lambda x:x x)(3)
(lambda x:x((lambda x:x(lambda (x(x)):x))))(lambda x:x)(lambda x:x x)(3)
(lambda x:x((lambda x:x(x))))(lambda x:x)(lambda x:x x)(3)
(lambda x:x(x(x)))(lambda x:x)(lambda x:x x)(3)
(lambda x:x(x))(lambda x:x)(lambda x:x x)(3)
x(lambda x:x)(lambda x:x x)(3)
x(x)(lambda x:x x)(3)
x((lambda x:x x)(3))
x(3 3)
3 3
6

'''

  • Related