A Functor in Haskell is a type class that defines a function fmap with the type signature (a -> b) -> F a -> F b, which generalizes the map function to work on any data structure; it takes a function and a container (like a list or tree) and applies that function to every element inside, returning a new container with the transformed elements, enabling consistent element-wise operations across different data structures.
Haskell Functors Explained: fmap and Type Constructors Tutorial
Added:hi this is Caleb I'm going to be talking about the uh functional programming language hasle today uh in particular I'm going to be talking about the topic of functors which can be a little bit challenging uh to some and I'm going to be uh going on a little bit about how I came to understand it and and and get to the point where I felt comfortable with using them um prior knowledge to this uh expected before you watch this video is uh basic uh computations and understanding of how lists work in hcll uh including like infinite lists and that things of that sort and zipping lists together uh functions higher order partial application of them CED functions composition of functions pattern matching building functions that kind of stuff um but if you got all that uh uh then and you want to learn about fun now then this is the video for you so as as you can see I've got ghci the interactive Haso promp over here open and as well as the notepad++ uh my not my my text editor I got some content in there examples and things of the sort and by the way of examples uh all of my examples were uh taken from uh learn you a hll and a a post on stack Overflow uh both of those links are in the description so if you want to check them out for yourself you can um but without further Ado I'll go ahead and get into it so as you you can see uh the definition of a functor uh in hll is class functor F where fmap has Type A to B to F to a to F to B so what this is saying is we have this function fmap here is the type definition for it um but we're not defining it yet that's what a funtor is so it takes a function that takes a type A and results in a type B and it takes an F of a and then it gives you an F of B so this F is kind of mysterious right now not really sure what's going on with that F but it it looks familiar as we can see right now f is taking as its argument a over here and B over here so F must be a function that takes a type another name for this in hasle is a type Constructor so now we need to know what what F returns um so let's let's look at an example of a type Constructor um maybe okay data Maybe takes type A and it gives you nothing or just a and these here these are types so we have the type nothing we have type just a so let's go ahead and see or these These are values I'm sorry these are not types these are values but let's let's see the type of nothing nothing has Type maybe a all right makes sense let's see the type of just five in fact let's do it this way so it's a little cleaner to look at just five so yeah there's just five let's see the type of it uh it is a maybe integer right because five is an integer let's see the type of this it's a maybe string right all right so basically a type Constructor takes a type and gives you back a type a way that we can look at this at the ghi ghci prompt is with the kind flag or just K uh so K of maybe for instance tells us that maybe is a regular type to a regular type all right so let's let's see how this how this relates to like int or Char right or bu these are all top level types right they're just types they're just kind of like values values are just values know value of five it's just a where a is a num all right or but then the value of a function uh would have been you know A to B or something which in our case k of maybe is a function it takes a type and it gives you a type all right so that's what Funk door needs from us so let's look at one implementation of fmap which is map from the lists so a little refresher map of odd onto this here would be you know give me tell me which of these are odd in the list and give me a list of those those values right so it took a list 1 2 3 4 five and it said it asked each one are you odd and if it if it was returned true or false and it gave us a list of the results map of a section say plus two on would result in 1 + 2 2 plus 2 3 plus 2 4 plus two 5 plus two and it would give us that list now that's you know the type of map if you look at it looks very much like the type of functor all right the type of fmap sorry type of fmap so what what is it about fmap that is similar to map so the relationship between them is that map is a a specific implementation of fmap for lists all right so fmap is the generalization of that so what do I mean by that what else would you act on well a list is a data structure right so say you had a tree right what if you wanted to apply a function to every element of that tree you couldn't do it with map that's where fmap comes in fmap says here's a function and here is here is a data structure of elements apply that function to everything in that data structure and give me back that data structure so fmap is going to need to know a couple of things it's going to need to know how to Traverse that data structure in order to apply that function so let's go ahead and take a look at a simple use of that down here all right so funter and tree right so we're going to say tree is either empty tree or it's node with a value and then another tree and another tree the left sub tree and the right sub tree so here's how it's going to happen tree is a a type Constructor right fmap is going to take a function that we don't care about because we're not going to use them and empty tree and it's going to return an empty tree that's the recursive base case if we have fmap and a function applied to a node with a value in it and a left sub tree and a right sub tree we're going to return the node with the function applied to that value and the recursive F map of the function applied to left sub tree and the function applied to the right sub tree and those will eventually reach the empty tree case and we'll get a full tree of all our values uh changed so if we had the 1 2 3 4 5 in a binary tree we could f f map over them with the odd with odd and we would pass the tree and then it would return to us a new tree filled with booleans you know so what wherever each element was if that element was odd it'll be true if it's not it'll be false so all fmap is is a generalization of map in order to work with all data structures really you know fmap came first and map was a specific use of that for lists because lists are so useful right so that's that's all that funtor is I get a lot of people talking about a box in the in the thing and I did come up with a box example but after I realized the data structure uh uh example with the tree uh it wasn't really that useful um but pause the video and take a look at this if you really want to uh this is just uh another implementation of of maybe honestly so uh that's all I have for for this topic um the only other thing that's really interesting about these that you can get into is working with uh IO input output and actions um because you can you can simplify some of that process and modify input from the user uh in one line which is kind of cool but it starts getting into things like mon ads and and some fancier notation uh which I think would be better put in another video so um yeah thanks for watching and I hope you learned a little bit more about functors and hasell thank you
Up Next

Profunctor Optics: Categorical Foundations with Bartosz Milewski
@LambdaWorld
14.8K views•2017-11-06

BitTorrent Protocol Explained: Piece Selection & Peer Choking
@StevenGordonAU
481 views•2013-02-22

HTTP Requests Explained: GET, POST, PUT, DELETE
@codecademy
103.1K views•2021-10-07

Enigma Machine Mechanics: WWII Encryption Explained
@JaredOwen
13.2M views•2021-12-11
Related Study Plans & Knowledge Roadmaps
Structured learning paths in Computer Science



































![Learn Haskell - fp-course - Applicatives - [Part 4]](https://i.ytimg.com/vi/-JEyYsirsMg/maxresdefault.jpg)
