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
3
u/Servious Apr 06 '22 edited Apr 06 '22
What makes
<*>associated with applicative (the function itself is not an applicative) is that it is defined in theApplicativetypeclass in Haskell. (link) Same with fmap and Functor.I think maybe what's happening here is if you haven't seen many typeclasses before this can be somewhat confusing. Essentially there exists a definition somewhere that says "hey if you want your type
f(Maybe, for example) to "be" a Functor you need to implement thefmapfunction for it." In haskell that sentence looks like this:For
Maybethe instance looks like this:If I were to lookup the type of
fmapI would get the definition I gave before. You'll notice that in this typeclass definition, there is noFunctor f => ...part infmap's type. That's becausefis already defined in theclass Functor fportion of the typeclass declaration. Probably the same thing happened when you looked up the applicative operator; it didn't include thatfneeds to beApplicativein the function type because that's already included in the fact that it's theApplicativedefinition. I'm not wording this well but I hope it makes some sense. The definition for<*>includesFunctorbecause applicatives need to be functors first. Every applicative is a functor.And for the second half of your question I'll just break down what the whole function definition means.
(<*>)- the name of our functionFunctor f =>- This is not an argument to the function. It is saying "hey we're going to use this genericFunctortype that could be anything that implements theFunctortypeclass and we're going to call itf." Again, not an argument to the function. If you use Java you can think of it like the<T extends Functor>portion of a method declaration.f (a -> b)- this is the first argument; it's saying we will take a function fromatobwrapped in a functor (very weird I know but I will elaborate later)f a- this is the second argument; it's anawrapped in the same functor as before.and finally
f bis the result of our function; abwrapped in our functorf.So the way this works is pretty interesting and complicated so I'll use a concrete example. Let's say we have a function
Now if we wanted to use it with
Maybes (Applicatives, because there is anApplicativeinstance forMaybe), we can doSo let's break down how this works. First,
sum3 <$> a. In this case,sum3needs to fit into the(a -> b)formatfmapexpects. Because of currying, haskell does it like so:So you can think of it like
Int -> (Int -> Int -> Int). Because the result offmapis alwaysf b, our result will beMaybe (Int -> Int -> Int). Huh, a function wrapped in a functor. Sounds familiar right? This is where ourApplicativeinstance comes in. When we use the applicative operator to dosum3 <$> a <*> bwe now getSo you can (hopefully) see how each time we continue to add arguments, the size of the function wrapped in a functor gets smaller and smaller until eventually it's just the final wrapped value.
I know this is a bit more than you asked for, but like I said I love talking about this stuff lmao. Again, feel free to ask questions. I feel like this comment wasn't as clear as my previous one and it was definitely more technical. And of course if I didn't really understand/answer your question and just rambled aimlessly PLEASE let me know lol