3 ms·
OK, the basic idea is that you have two functions, `f_1` and `f_2`, of type `a -> a`, and you want to run them both on an input value `x` to get an output `y`.
by yawaramin 10y ago
OK, the basic idea is that you have two functions, `f_1` and `f_2`, of type `a -> a`, and you want to run them both on an input value `x` to get an output `y`. You can think of `f_1` and `f_2` as a data pipeline that `x` passes through and then the output comes out the other end. So,
y1 = f_1 x
y = f_2 y1
-- or,
y = f_2 (f_1 x)
But what if you have an arbitrary list of functions as your pipeline?
y = f_n (f_{n-1} (... (f_2 (f_1 x)) ...))
Well, the solution is that functions of type `a -> a` are composeable, so you can 'add them up' as if they were real values (which they are). The Haskell function composition operator is `.`, so:
y = f_2 (f_1 x)
-- is the same as,
y = (f_2 . f_1) x
Now, how do you go from 'composing two functions' to 'composing a list of functions'? Well, monoids are a typeclass (a statically-enforceable design pattern) that encode the idea of being able to combine two things into one, and 'for free' give you a function `mconcat` that combines a list of those things into one. So if you have a monoid instance for functions of type `a -> a`, you're now able to combine many of them together, get a single composed function, and apply your input value `x` to that.
`Endo` is just a fancy name for 'function of type `a -> a`'. I agree to a large extent that all this category theory stuff can get distracting. I personally think type theory is a much more rewarding field of study. To 'endomorphism', a type theorist would say, 'Oh, you mean `a -> a`?'
- lobster_johnson 10y agoThis is an extremely lucid answer, thank you. Can you please now rewrite all the Haskell documentation in the same way?
- jholman 10y ago+1 (I know "+1" posts are a sin, but if a few more haskellers take heed of the documentation problem, it'll be worth me getting hellbanned.)
- majewsky 10y agoThis. Documentation is the most frustrating part of the Haskell experience to me. More than once I've come across a module on Haddock and the module description just says This module is inspired by the following paper: <DOI>
- codygman 10y agoReposting my message to evincarofautumn to you as well: If you aren't planning on submitting that to those docs I will. In fact I'm sure many would find you adding examples along those lines to the most popular but math inspired Haskell libraries.
- yawaramin 10y agoHi, I'm not planning to, but please be my guest. One thing I would recommend is to put examples in a collapsible Haddock section as described in https://github.com/haskell/haddock/issues/335 https://github.com/haskell/haddock/issues/335 so readers don't have to skim through it while looking up API references.
- stcredzero 10y agoIf, in such threads, a few posts up from the point analogous to this point, people just started talking about composing functions, and composing lists of functions, then more understanding would be generated.