Transcript from the "List Operations Solution: add & constant" Lesson
>> Kyle Simpson: All right, well, I trust that exercise was a bit challenging, but hopefully you got a sense of pulling in a variety of different things. Let's walk through it and make sure we catch all of the details. So item number one in the read me says that we need to define two different functions each of which return a value.
[00:00:18] I'm gonna use numbers just to be very convenient. So I'll define a function five, that returns the number 5. And then I'll define a function nine, that returns the number 9, for no particular reason just because that's what I made up at this point, okay? That's what item number one in the read me says.
[00:00:39] Item number two in the read me said to create an add function which takes two numbers. Well, that should be pretty straightforward. We take an add of an x and y, and it just adds those two together, and it gives you the value back. Now, how would we use add with our five and our nine?
[00:00:56] Well, we would call that by saying add, and then we'd have to call the function five and call the function nine and pass in their values. What's that called when a function returns its result and that immediately becomes the input to another function? Composition, that's right, okay. Just making sure you're still at least partly awake, okay.
[00:01:21] Item number three says we wanna create a function that can take functions and call them. So what did we say, we'll call that add to, that's what the readme says. We'll make an add2 function, and that takes an fn1 and an fn2. And it needs to use the add utility, so we're just gonna literally call it like we called it there, but with the fn1 and the fn2.
>> Kyle Simpson: That's sort of a higher order add utility, isn't it? It doesn't add numbers, it adds functions that hold numbers.
>> Kyle Simpson: Okay, item number four said take that five and that nine function that we defined in step one and let's replace those with another utility. A utility which I'm going to call constant, and I'll explain in just a moment why.
[00:02:23] That utility needs to take in a value and return back a function, that when called, that function returns the initial value.
>> Kyle Simpson: So it's like taking a value and wrapping it up in a function closure. And I called this utility constant because that's exactly what that's called in functional programming libraries.
[00:02:48] When you wanna make a function wrapped around a value, it's called constant. So how could I define a five? I could say five = constant(5) and nine = constant(9). And then if I wanted to use that add2 utility with both of those, then that would look like add2, and I'd call five and nine.