1 00:00:08,030 --> 00:00:12,585 Lambda expressions are an alternative way of defining functions. 2 00:00:12,585 --> 00:00:18,480 So, suppose that we have this function definition and here I'm using args, 3 00:00:18,480 --> 00:00:20,460 as a stand-in for a list of arguments, 4 00:00:20,460 --> 00:00:24,195 so I might have x, y, z. 5 00:00:24,195 --> 00:00:27,150 These arguments might even have default values, 6 00:00:27,150 --> 00:00:30,660 I might have x equals one etc., 7 00:00:30,660 --> 00:00:33,989 and we have some return value expression. 8 00:00:33,989 --> 00:00:41,955 So, that might be return x plus five or any expression that we want to return. 9 00:00:41,955 --> 00:00:44,695 If we have that kind of function, 10 00:00:44,695 --> 00:00:48,230 then we can express it using a Lambda expression. 11 00:00:48,230 --> 00:00:50,465 This is a Lambda expression. 12 00:00:50,465 --> 00:00:54,110 Whatever we wrote for our list of arguments here, 13 00:00:54,110 --> 00:00:58,505 we could transplant to our Lambda expression and reuse it down here. 14 00:00:58,505 --> 00:01:01,469 So, we could have Lambda, 15 00:01:02,470 --> 00:01:08,605 and then our argument might be x and y, 16 00:01:08,605 --> 00:01:14,179 and then we say colon and then whatever value we want to return, 17 00:01:14,179 --> 00:01:16,850 we can again specify in our Lambda. 18 00:01:16,850 --> 00:01:25,500 So, we might say return x plus y. 19 00:01:25,900 --> 00:01:32,110 This expression, has a value whose type is a function. 20 00:01:32,110 --> 00:01:35,065 We don't even need to give this function a name, 21 00:01:35,065 --> 00:01:36,970 like we do when we use def. 22 00:01:36,970 --> 00:01:38,590 If we do want to give it a name, 23 00:01:38,590 --> 00:01:43,325 we can say func equals and then provide our Lambda function, 24 00:01:43,325 --> 00:01:46,375 but we can also make it an anonymous function, 25 00:01:46,375 --> 00:01:49,525 in other words, a function that doesn't have a name. 26 00:01:49,525 --> 00:01:52,570 So, let's look at some code examples. 27 00:01:52,570 --> 00:01:54,475 Here on line one, 28 00:01:54,475 --> 00:01:56,065 we define a function f, 29 00:01:56,065 --> 00:02:00,790 it takes in one argument x and it returns x minus one. 30 00:02:00,790 --> 00:02:04,765 Again, this is kind of the traditional way of defining a function. 31 00:02:04,765 --> 00:02:10,925 If we called our function by saying print f called with three, 32 00:02:10,925 --> 00:02:13,665 then we should get the value two. 33 00:02:13,665 --> 00:02:18,485 If we print out the function object itself by saying print f, 34 00:02:18,485 --> 00:02:22,570 then we should get that this is a function named f, 35 00:02:22,570 --> 00:02:29,090 and if we print out the type of f by saying print the type of f, 36 00:02:29,090 --> 00:02:33,365 then we should get that f is a function. 37 00:02:33,365 --> 00:02:38,640 Now, if we wanted to write this function with an equivalent Lambda expression, 38 00:02:38,640 --> 00:02:43,095 then we would say Lambda x, 39 00:02:43,095 --> 00:02:47,475 which is our argument, and then we went to return x minus one, 40 00:02:47,475 --> 00:02:49,890 so we just write x minus one. 41 00:02:49,890 --> 00:02:54,335 Again, we don't need to add a return statement for Lambda expressions. 42 00:02:54,335 --> 00:02:58,175 In Lambda, the return is implicit. 43 00:02:58,175 --> 00:03:02,795 So, if we print out the value of this expression, 44 00:03:02,795 --> 00:03:06,269 then we should see that it's a function, 45 00:03:06,269 --> 00:03:10,150 so in this case, it's a Lambda function specifically. 46 00:03:10,370 --> 00:03:18,660 If I instead say lf equals this value, 47 00:03:18,660 --> 00:03:23,220 and if I print out lf called with three, 48 00:03:23,220 --> 00:03:26,110 then we should get two. 49 00:03:26,330 --> 00:03:33,075 If I print out the type of this Lambda function, 50 00:03:33,075 --> 00:03:36,480 then we should see that just like f, 51 00:03:36,480 --> 00:03:40,435 our Lambda function lf is a function. 52 00:03:40,435 --> 00:03:45,304 So suppose that we wanted to convert this traditional function definition, 53 00:03:45,304 --> 00:03:48,275 called last char, into a Lambda function, 54 00:03:48,275 --> 00:03:54,454 then what we could do is we could say last char equals a Lambda, 55 00:03:54,454 --> 00:03:56,930 it takes in one argument s, 56 00:03:56,930 --> 00:04:00,200 so Lambda s colon, 57 00:04:00,200 --> 00:04:04,190 and then again remember the return is implicit with Lambda, 58 00:04:04,190 --> 00:04:08,520 so we can just say s sub negative one. 59 00:04:08,710 --> 00:04:14,199 Now, the value of this expression is a function, 60 00:04:14,199 --> 00:04:17,250 which is equivalent to this function. 61 00:04:17,250 --> 00:04:20,450 We don't necessarily need to actually give it a name, 62 00:04:20,450 --> 00:04:23,685 but here we chose to give it the name last char. 63 00:04:23,685 --> 00:04:28,535 So, let's answer some multiple choice questions about Lambda functions. 64 00:04:28,535 --> 00:04:31,940 So, this question asks if the input to 65 00:04:31,940 --> 00:04:35,255 this Lambda function is a number, then what's returned? 66 00:04:35,255 --> 00:04:38,284 So, here we have a Lambda expression, 67 00:04:38,284 --> 00:04:40,805 it takes in one argument x, 68 00:04:40,805 --> 00:04:43,465 and it returns negative x. 69 00:04:43,465 --> 00:04:45,090 So, what that tells me, 70 00:04:45,090 --> 00:04:47,325 is that if we pass in one, 71 00:04:47,325 --> 00:04:49,120 we should get negative one, 72 00:04:49,120 --> 00:04:50,925 if we pass in negative 10, 73 00:04:50,925 --> 00:04:52,275 we should get positive 10, 74 00:04:52,275 --> 00:04:55,040 etc., and so that to me is B, 75 00:04:55,040 --> 00:04:57,110 a number of the opposite sign so 76 00:04:57,110 --> 00:05:00,535 positive numbers become negative negative numbers become positive. 77 00:05:00,535 --> 00:05:03,300 So, i would answer B. 78 00:05:03,500 --> 00:05:07,270 That's all for now, until next time.