r/learnprogramming • u/Temporary-Warthog250 • Apr 05 '22
Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them
Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.
Someone else who reads yours and does understand it, try to explain it to them to help them learn
1.6k
Upvotes
96
u/Servious Apr 05 '22 edited Apr 06 '22
IMO these things are best understood through the lens of Haskell's
Maybetype.Very simply, a
Maybe Intfor example can be eitherJust 5orNothing. At its most basic form, it can be used with pattern matching. For example here's a haskell function that adds 2 to a Maybe if it's there:You can see we had to use pattern matching to "unwrap" the actual value of the maybe so we could add 2 to it. This is pretty inconvenient and pretty annoying especially if you're trying to do something more complex than simply adding 2. That's where
Functorcomes in.Functorallows you to apply a function to the value inside the functor and get back the modified functor by using thefmapfunction. Here's what that definition looks like:This definition is a bit complex so I'll break it down. This is saying "fmap is a function which is defined for any
Functorf. This function accepts a different function fromatob; and a functor containing ana; and returns a functor containing ab."aandbcan be any type. What makes something a functor at the very base level is whether or not this function is defined. You can think about it like mapping over a list in any other language because that's exactly whatfmapis defined as for lists.So in our
Maybeexample, we can use it like so:Much more convenient, and as an added bonus it looks a bit more like regular function application (in haskell anyway).
We're not quite to monads yet because there's a step between
FunctorandMonadand that's calledApplicative. Instead of starting with the (somewhat confusing) definition, let me pose a question: what if I have a function with more than one argument that I want to passMaybes into? Like what if I wanted to add twoMaybevalues together? That's the problem applicative solves.With functors we can do:
But with an applicative instance we can do:
the result of which will be a
Maybecontaining the result of adding the two values inside. If either of theMaybevalues areNothingit will come out asNothing. And this pattern is extendable for more than just two arguments as well. For example:So to quickly summarize: functors allow you to apply a function to the inside of one "wrapped" value (like
Maybe). Applicatives allow you to apply a function of many arguments to many "wrapped" values. Now, here's where we get to monads. Here's a situation you might be in:How would you feed that
someMaybevalue into thesomeFnfunction? You might guess to usefmapfrom functor, but let's look at what would happen:Oof, double-wrapped
Maybe; That's not right. This is the problemMonadsolves with thebindfunction or>>=operator. The correct code for this would be:Also worth noting that for something to be a
Monadit must also beApplicativeand for something to beApplicativeit must also be aFunctor. So all monads are also applicatives and functors.Sorry if you don't know any Haskell because this is pretty complex and very haskell-focused, but I kinda figured you wouldn't be asking these questions without a passing familiarity with some haskell. Feel free to ask any questions; I love talking about this stuff!