+ 1

Is it possible to write this code line with lambda function ? If it is how so?

https://code.sololearn.com/cYDhJ2aL2GyX/?ref=app

26th Oct 2019, 8:09 PM
Baran Aldemir
Baran Aldemir - avatar
11 Réponses
0
Anonymous function lambda x: x+5
27th Oct 2019, 12:18 AM
Baran Aldemir
Baran Aldemir - avatar
0
lambda functions have nothing to do with lambda calculus.
27th Oct 2019, 1:59 AM
Rora
0
Okay, I've changed the question. I read that lambda functions get their name from lambda calculus.That's why I asked it in that way.
27th Oct 2019, 7:13 AM
Baran Aldemir
Baran Aldemir - avatar
0
Mirielle🐶 [Inactive] I couldn't understand how your expression work. Probably because I have a misconception about lambda functions. How come your expression work and Print((lambda x: x+5)([1,3,5])) doesn't?
27th Oct 2019, 9:55 AM
Baran Aldemir
Baran Aldemir - avatar
0
Baran Aldemir it’s the for loop. You need to iterate through the values and add 5 to each one, and for that you need a for loop (list comp).
27th Oct 2019, 11:22 AM
Rora
0
Rora So, lambda has to take values one by one. It can not deal with a list at once?
27th Oct 2019, 12:45 PM
Baran Aldemir
Baran Aldemir - avatar
0
Mirielle🐶 [Inactive] Have you you tried it? Because I get the error below. TypeError: can only concatenate list (not "int") to list
27th Oct 2019, 6:18 PM
Baran Aldemir
Baran Aldemir - avatar
0
Mirielle🐶 [Inactive] I think I got it. If the lambda function returns an int, argument has to be int as well. If the lambda function returns a list, argument has to be list. Otherwise it can not concatenate. Because similar code to your previous code print((lambda x: [i+5] for i in x)([1,2,3])) works. Am I right?
27th Oct 2019, 6:29 PM
Baran Aldemir
Baran Aldemir - avatar