Lambda – Design of Computer Programs

Lambda – Design of Computer Programs


Let’s consider a formula–I’m going to write a new one. Let’s say You equals Me-squared. We’re treating these formulas as strings. Now when we substitute numbers into this, we get something like 123 equals 45-squared. What happens when we call eval on this string? What eval has to do is it takes it’s input, which is a string, and then it has a process of parsing that string into a parse tree. The parse tree would say something like this is a comparison, and it has an operator, which is a number, which is 123, and another operand, which is an expression which has the number 45, and then the exponentiation operator, and then the number 2. Python builds up a data structure that looks something like that. There’s another operation of code generation in which Python takes this tree and says in order to evaluate this tree I’m going to do something like load the number 123 and then load the number 45 and then do an operation on that and so on and so on and then return the result. That’s a lot of work to build up this tree, generate the code, and then finally, the final operation after we’ve come up with this, is to execute this code and come up with an answer, which in this case would be false. Now, this is a lot of work for eval to do, and it seems like there’s a lot of duplicate work, because we’re going to do this for every permutation of the digits, but each time we go through this part of the work, the parsing, is going to look exactly the same with the exception of the specific numbers down here at the bottom, but the rest of the parse tree is going to look the same. Similarly, this part of the work, the code generation, will also look the same except these numbers will differ. We’re going to have to repeat that over and over again. What we’d like is an approach where we can only do these two parts once and then pass in the specific numbers and then get our final result back. But one this I should say is that the eval function doesn’t take a statement, like this function definition. It takes an expression. Furthermore, we don’t really need this name F. That was kind of arbitrary. We’d be fine with having an expression that represents a function but doesn’t have a name associated with it. Then we can pass that directly to eval. It turns out there is a way to do that in Python. There is a keyword that creates a function as an expression. That keyword is lambda. It’s kind of a funny keyword. I would’ve preferred them to use function or fun or maybe just def and leave the name out, but they chose lambda. It’s based on the lambda calculus, the Greek letter λ, which is a branch of formal mathematics defining functions. The syntax is fairly similar to the syntax of a definition of a function, except we leave the name out. It’s an anonymous function with no name. It also turns out for some reason we leave out the parentheses. We just say lambda Y, O, U, M, E, then a colon, and we leave out the return, and then we just put in the rest of the code–100 times blah, blah, blah, blah.

Leave a Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Copyright © 2019 Geted Tabs Online. All rights reserved.