Placeholder Image

字幕表 動画を再生する

  • So a while ago, we talked about the Lunda calculus, which is a simple but powerful mathematical theory of functions.

  • But something we didn't talk about at the time was the idea of curried functions, which sound a bit spicy but are actually very useful.

  • So that's what we're gonna talk about today.

  • Curried functions as we often do.

  • We're gonna start off with a question, and the question is what actually is a function.

  • So for me, a function simply takes an input on one side, processes inside and then gives an output on the right hand side so you can think of it as being a little box or little machine takes an input on one side, processes inside the box and then produces some output on the other side.

  • So let's have a simple example of a function on a CZ.

  • Usual.

  • I'm gonna be using Haskell, but you convey, basically, do everything I'm going to show you today in any modern programming language that provide some support for the London calculus.

  • So example I'm going to start with is a simple increment function on.

  • We define it like this.

  • Incremental X is X plus well so just add one toe a number so you can kind of have a look at the anatomy of this.

  • So the structure of this on we're doing three things were defining a name for the function we're calling Inc for increment.

  • We're defining a name for the input promised her eggs on.

  • We're explaining how the output is calculated in terms off the infant so we can have a couple of examples of this.

  • We could increment the number one and we get to or we could increment the number two and get three.

  • So it's all very simple and very boring.

  • So let's move on towards curried functions.

  • Now on the first step we want to take is the idea of applying a function to a function as on in court.

  • Let me give you a simple example of this.

  • So what I'm gonna do isn't with a map the increment function that we just defined over the list from 1 to 10.

  • So what map is is it's a function which takes two parameters.

  • The first parameter is another function in this case, the little increments function that we just defined.

  • The second parameter is a list of things in this case, a list of numbers from 1 to 10.

  • And what Mom does is it takes to function here on applies it all the way across the list that were just simply incremental thing.

  • And all the numbers from 1 to 10 on Mum is called a higher order function because it takes a function as one of its infamous.

  • So let's have another example off something a bit more interesting.

  • Let's think about the idea off a function which has more than one input or more than one parameter so we could find a little function called Ad on.

  • We're gonna take two numbers x and y as inputs, and then we're going to simply add them together on the important point here is that we take the two numbers X and Y packaged up together as a pair at the same time.

  • Okay, so what we're gonna do then, for an example is we could add one and two and give three.

  • Or we could add two and three on give five.

  • So very simple on again.

  • Oh, very boring as well.

  • It's not really anything.

  • Anything new at the moment.

  • So what, then, is a curried function.

  • So a curried function is a function like ad, which takes more than one input.

  • But rather than taking them at the same time, Packet is a pier or a triple or something like that.

  • It takes its input one at a time.

  • So how could we redefine ad as a curried function?

  • What's very simple?

  • We just take the brackets act So rather than saying, Ad of X Y is X plus y in brackets, we just say out of X and then it's based on.

  • Then why is experts why?

  • So it looks basically the same, except we're taking the two inputs X and Y one at a time, no, rather than at the same time in a pair.

  • And then we can apply this function in exactly the same way as before.

  • And we just give it to inputs like one and two and I get three.

  • Or we could say, What's two and three and we get five.

  • Okay, so it looks basically exactly the same as the regular edition function, except rather than taking a pair of inputs at the same time, it takes to inputs one at a time so that you can say, Well, what's the point of defining functions in this curried manner?

  • Well, the point is that you don't need to give them all of their inputs.

  • So, for example, if we did add of one on, we see what it says.

  • We actually get an error here.

  • So where do we get an error?

  • So we get an error because this is still expecting its second parameter.

  • So if I apply, add to a single number one.

  • It doesn't know how to do the addition yet because we haven't given it a second number.

  • And that's what it's saying here is trying to print to function here.

  • So it says, I don't know how to show a function from integers integers or if you want a more slightly comprehensible messages, saying maybe you haven't applied a function to enough arguments.

  • Okay, so with the current function, which takes its input one at the time, you can partially apply it to a subset of the inputs.

  • So what could you actually do with a function like add one?

  • Well, we did moppet, for example.

  • So here is a way off Incremental thing.

  • A list of numbers without defining a custom increment function.

  • So previously we defined.

  • Think of exes experts one and then we mapped it here.

  • We're using the ad function, which is curried because it takes its inputs one at the time.

  • And all we're doing is giving the ad function one parameter here.

  • And then it's a function which expects its second perimeter, and then it will map that all the way across the list.

  • So we get the same behavior using this general Purpose Edition function without having to define a custom increment function.

  • So you can ask yourself at this point what's actually going on with a curried function when I say it takes his inputs one at a time.

  • What does that actually mean?

  • So let's kind of look at this definition again.

  • So here's the definition we had out of X Y is X plus y.

  • What we can do is do a simple London calculus trick here rather than having the two inputs on the left hand side of the equals.

  • Let's move them across the equal sign to the right hand side of the definition, and we'll do this in two steps.

  • So what I'm gonna do is I'm gonna say Ad of X is London.

  • Why Arrow X plus why?

  • So what I've done here is the wine is no longer on the left hand side of the definition here.

  • No, it's on the right hand side of the definition.

  • So what this is saying is, if you add a number X, then what you get is a function which is waiting for a second input.

  • Why?

  • And then it's going to give you back X plus y on this expression on the right hand side here is called a lander expression.

  • It's a nameless function, and it's got the same kind of anatomy as a normal function definition, except for the fact that you don't give the function in name.

  • So we look at what's going on here.

  • We're taking an input parameter called Why We're giving back the result X plus y.

  • But nowhere in the blue box here have we actually given the function a name.

  • It's a nameless function, and then we can actually play the same game with the other input as well.

  • So rather than having X on the left hand side, we can move it across to the right hand side on here we have our definition or our ad function in a more primitive way, and this really lets his understand, in a quite fundamental way, what's going on with curried functions.

  • What we're saying is that the addition function is defined to be the function, which takes an infant called X, and what you get back is another function on that function.

  • Takes an infant cold.

  • Why and then gives you back the result X plus.

  • Why you can ask yourself, Where does this idea of currying come from?

  • Well, it's named after Haskell Curry, who was a mathematician on logician who studied these kind of things.

  • But Haskell Curry himself.

  • Hey, wasn't the person who actually invented this plantation on or this idea?

  • He attributes it to someone else called Moses, Shown Finkle, who was also a logician and mathematician working around the same time showing.

  • Think Ling is quite a hardware to say.

  • I've had to practice it quite a lot, even to be able to say it today.

  • It's not.

  • It's not a word, which is very easy to say.

  • Where is currying kind of just trips off the tongue.

  • It's a nice, easy word to say so.

  • Curry got the credit for it, but really probably Moses shown.

  • Finkel is the one who first studied this idea.

  • So just to wrap things up, I want to give you an example, which shows that, actually, curried functions are quite natural in the real world as well, Not just in computer science.

  • So we think about a cash machine.

  • What is it, cash machine do?

  • Well, it takes three inputs.

  • Typically, you put your card in, you put your pin number and on you put a request in on.

  • Then it's going to give you some response, which usually is taking some money.

  • Let's make this a generous cash machine.

  • Let's decide that.

  • It's always just gonna give you £100 or $100.

  • But this is no actually how cash machines work.

  • When you go up to a cash machine, you don't put all of the three inputs in at exactly the same time.

  • Well, maybe on the Saturday night when you've been out trying for all the three inputs at the same time.

  • But this is no what happens in practice.

  • You put your card in, then you put your pin number in then you put your request in, and finally the machine's gonna give you some money, and that's because it's really a curried function.

  • So let me redefine it like that.

  • So here we're defining as a curried function.

  • It takes its input one at the time, so we take a card than a pin number than a request.

  • Actually, we could use a long limitation to make this even more clear.

  • What's going on?

  • What we do is we take card when we get back, a function that takes a pin number and then we're going to take a request on.

  • Then we're going to get back our 100 points.

  • So for me, this definition down the bottom here really captures the essence off what a cash machine is as our current function.

  • So it's a function that takes your card as an input on.

  • What it gives you back is a function that expects your pin number on that function takes your pin numbers and input.

  • And then what you get back is a function that takes a request, and finally, when you get the request, the machine will be very generous and just give us our £100 or your $100.

  • So that's really all I want to say today about courage functions.

  • They're very simple idea its functions with multiple inputs that you define in a way that the inputs come one at the time rather than altogether.

  • And as I said at the start, they sound a bit spicy, but they're actually very simple and very useful function.

  • Broadly, languages take care off.

  • Ah, lot of the implementation details that older programming languages you have to do manually, for example, memory management.

  • But now it is.

  • It's very popular to use.

  • Language is like even job, for example, which builds memory management into the programming language function.

  • Languages do that, too.

So a while ago, we talked about the Lunda calculus, which is a simple but powerful mathematical theory of functions.

字幕と単語

ワンタップで英和辞典検索 単語をクリックすると、意味が表示されます

A2 初級

カレー関数 - コンピュータマニア (Curried Functions - Computerphile)

  • 6 0
    林宜悉 に公開 2021 年 01 月 14 日
動画の中の単語