 Can everybody see it? I'm Paul. I'm going to talk about polymorphism 101. We promise to have a 101 session for every talk. And I just used Sunkist style to use terminal presentation. So first, that's polymorphism is that we have kind of interface so that we could reuse the copyright. Saying that we want to replicate something with just integer, then we just replicate a number three times. It's in dot pass. And so replicating all of them will give us three elements, right? But if I want to reuse this function, then it would be better to pass in anything and replace the same thing for three times. So how to make it into polymorphic? Like saying that we want to pass in anything beside integer, we could just specify a type variable for any character you use. And with that, I could define a replica of polymorphism and for that, then I want to replicate true for three times, replicate poly for three times, or replicate 1.0 for three times. That could be anything you want to pass in by still integer will work, right? So specifying a parameter as a type variable, that will make the function polymorphic directly. So because with type variable, that means it will serve as a placeholder for type information. So that means without any constraint, we don't know about what you are going to put in. So for the implementation itself, it also assumes nothing about it because it's pretty straightforward, just calling itself and pre-pending the element to the head of the list. So with this information, we don't need the object, any information about the object itself, right? Because we just put something we don't know yet to the head of the list, OK? And the AC both could be anything. You could want to make an emoticon like that. That would also work, as you want to add some constraint for the object you pass it in. And with this, I'm going to introduce a concept called type class. But I'm going to just introduce how to use it without presenting how to define it. That would be next one-on-one session. So with print function, that means we want the thing passing in to be printable on the screen. That means the object could be encoded into a string. And the string itself could be put into the console. So we show which is defined in the prelude. So that means we construct the A passing with this parameter to be showable. That show would have its building definition of how to convert an object into a string. And with that string, we could print it out to the screen like passing with 3.14159 as double. Then with double, that is building inside the previews. So we could know that is printable in the print. So besides show, there are a few of type class that means constraint here to use. If I want to implement a function that's checking if the element exists in a list, like I'm passing an element, which is here, the E element, and passing another list, I want to return true for the element exists in the list and false without the element in the list. So it's pretty straightforward to define a recursive definition without any element and just return false. And with pattern matching, add just the head of the list and comparing the head with the element passing. If it's equal, then return true. Otherwise, return false. But with that, there's a presumption here is that the A have to be comparable to be equal or not. So we add an EQ, means equation for A. That means the A element passing, we have to work on an equal operator on that. Without that, we can't just assume there's an equal operator in them here. So with that, we return true because one is in the list right, otherwise, then it's false. So with these two examples, you could also constraint the element passing by type parameter. So this is for the function implementation, especially for pure function. But with the data structure, you define that you could also use type parameter. Here's A that in the previous one line session, we know how to define a recursive data algebraic type right. With this, it's either a now or the rest of this element. So this is our user defined list. It's not built inside the compiler. The A could be anything. So this is a polymorphic list. Then with the left implementation, then I'm just adding one for each of the elements using type parameter. One side bounding to accounts, then adding one and calculate the rest of the list. This is implementation also not related to the element itself, because I'm just calculating how many elements inside. I have no information about the element itself, but I have information about the list. So this define. And I could change it to other stuff like flow double or then evolve into anything about the element in the patient. So A could be anything. And we didn't add any constraint on that. So yeah. So here, basically, it's the definition. And so beside specifying type parameter in the signature, the one thing special to Haskell is that we have higher order thing. That's sort of you can specify the information on the underlying structure that, say, we have a higher order function called map. That's just changing the underlying structure's elements. So with given list, which is structure, then I could transfer it into another list. But without any information about the A, and later on in the later of the talk, I would also say that the list could also be changed with the higher order structure defined. So map means that just given a function, then it could work on the element type A. And with that fx applied, then the output of the function will be type B. Reconstruct the list again to be the transformed list of type B. So with this define, and I could just say I want to check if the list is odd, then it will transform the list into the result of whether it's an odd number or not. It's true and false and true. With the A, it's defined as 1, 2, 3. Any questions so far? A bit harder for that is phone. That's something just mentioned about the phone on one side. That phone is reduced in list or Ruby. That given a list of that binary operator, that you like, squash it into the result. That with 1, 2, 3, and plus, then we just sign up the list. And there's two versions. One is make the operator map associative. That means just apply the function first. If you draw into the abstracting texture, that means apply the parenthesis to the left side first, then to the next one. So with this recursive define, then we could just fold everything with that. So fold the associated. And start an initial value with 0, then I could get the sum of 6. And another version is make the phone right associative. Because the false is a higher order concept that we could use it for redefine the end of the function we previously defined. The right is basically the same as left associated. If you draw into an abstracting texture, it's just you have a parenthesis for the right-hand side first, then keep going until the left end. So you could see that you could just use, you recursive apply to the function itself, then call the function passing at the outer most. So with that structure, that means we skew to the end of the list, then getting forward, then finally get our result. And with these two functions define that higher order concept, we could use that function to redefine any. So with fold out, it's the building definition for a phone left associated. And with that, with any, then we just passing a function, which is the result so far and the current element. We want to order it's a shortcut override. So with the current result, and comparing it to the current element, whether they are equal. If they are equal, then means the element exists in the list. And start with the false. So any one of them comparing equal, then the whole list will be true. Otherwise, keep going, then it will be false. So it's the same implementation. But I could reuse the concept, the folding concept, that it could, in my opinion, is also kind of polymorphism. Because I could use it with type class to foldable. It's built in the premium library. That means the container with a folding defined. I can't strength. It's a metaphor, not exact metaphor, but it's easier for comprehension. It's no longer a list here, right? It's the, comparing to the, here is an list, right? But here I specify a T. And the T, I also kind of strain, adding a constraint to let it to be foldable. And with the T defined, as well as the structure or the container dosing passing, define the fold function, then you could reuse this function. With this defined, I could still reuse any fold one. Because the list is half a fold, already defining the standard library. So that's somehow generalized any to another level. And I didn't define here, I promise, for animated. For map, you could also use map specifying it's a spunker T with the same definition, so far. Funker means that you could, sorry, Funker means the structure is have a map defined on it. Then with this, it's also defined, with the existing list, it's already defined the standard library. But this, I didn't define it. But with that specifying that, you could also make the map we use for any container have a map defined. So that's all. It's not too complicated for, especially for foldable. Is it OK? Like foldable means you specify a constraint that you could squash the structure into a binary operator into one final value. Yeah, but because they involve the type class, I would rather to leave it for the next session. Yeah, it's a trade-off that if I involve too much, it's too much that puts, like, involve too much to involve type class. So I only teach how to use it without defining. Because with that, you have to define either a folder or a folder map or folder on the underlying container. It's right. It depends on the strictness of the emulation, that there are a few versions of fold, that there are a strict version of fold l, and which would apply this function first before expanding the structure. And for this version, it's like, without evaluating this function and expanding the whole list to the end. It had to be called pyramid. Yeah. I could just derive the folder before our customers. Like, full managed, we don't have it. But could we just derive it? Not pretty sure. Is there an answer to that? Could we define foldable for a user because they define an algebraic? I think yes, yeah. They could just try. There's an extension for that. Should I add something? I would just do it. I would just do that. Derive foldable. Yeah, yeah. Derive. Set, but you load the source file, or you copy and paste a bit. You didn't load the source file. Because it's just CI, it's another session. OK, you could say that. .task, or whatever. What you can do is call on set minus x, derive foldable, that would work in general. Call on set, set, set. And then you can skip arbitrary, derive, fold. Just be socializing. I don't know beyond having anything that maybe we could move to the hawker center if we wanted to take this place down. Yeah, this is just someone out there with the tables. But we do need to go around a lot. Yeah. I've got a plan. Put your tables back in and go to the box. I'll be right back. I'll see you in a couple of minutes. OK. OK. I'll be right back. Put yourself in my pocket. I'll be right back. Do you still have to look at that? What would be your priority? What would be your editor? Because eventually, I'll have some time to talk about the behind the sign. Yeah. Yeah. Right? No. It's working. Let's see if you can figure out how to turn that on. It's nice to be loud. Yeah.