3 ms·
There are many styles of functional programming; the thing they have in common is that they all use first-class, high-level functions to implement features whic
by chriswarbo 11y ago
There are many styles of functional programming; the thing they have in common is that they all use first-class, high-level functions to implement features which other languages might implement via special-purpose syntax, bytecode, compiler passes, etc.
The "lambda the ultimate" papers are the classic example of using functions to implement a variety of language features, including jumps. Hence the author's claim that functional programming is an "inside-out" approach is more of a question of style than of paradigm.
For example, even simple function composition can approach the clarity of imperative code for following a step-by-step process. The result tends to look like UNIX pipes, although traditionally they're read "right to left" (but you can trivially define your own composition function which goes the other way).
Here's an example of the author's task, written in a compositional style rather than an "inside out" style:
heatedOven = preheat 175 oven
preparedPans = grease . flour $ pans
flourMixture = whisk . add flour . add bakingSoda . add salt $ smallBowl
creamedMixture = until (all [light, fluffy]) . cream . add butter . add whiteSugar . add brownSugar $ largeBowl
bananaMixture = mix . add bananas . repeat eggCount (beat . add egg) $ creamedMixture
batter = stir . add (chop walnuts) . repeat (size / spoonful) (add (spoonful flourMixture) . add (spoonful buttermilk)) $ bananaMixture
baked = take . wait 30 . add (add batter preparedPans) $ heatedOven
cake = take . wait 10 . add baked $ dampen teaTowel
- cwyers 11y agoUgh, I am sorry, I tried to upvote but I fatfingered it and downvoted instead.
- paulddraper 11y agoIt's a shame you can't fix it.
- krotton 11y agoIt's a shame you can't fix.
- greydius 11y agoIf you create a Kitchen monad, you could write it imperatively. Then you can have your functional cake and eat it too. (I couldn't stop myself.)
- tmptmp 11y agoa "kitchen monad" may seem to be a overkill for doing what you (or your mom) may need to bake a small number of cakes for you to consume but bakery monads are what is needed if you want to bake hundreds of thousands of cakes of hundreds of different variations (types) in a manageable manner. FP starts to make (more and obvious) sense there not in the small household kitchen. But even then that is not to say that FP doesn't make sense in a small household kitchen. Only, it is very difficult to make sense of that sense at the smaller scales.
- greydius 11y agoI was just attempting to inject a little humor into the situation while bringing attention to the fact functional and imperative are not necessarily mutually exclusive. In fact, I'm of the opinion that Haskell is the best imperative programming language. It's just really hard to explain why that is the case when the popular sentiment is that monads are scary and impossible to understand. In any case, these threads always devolve into ideological bickering which I try to avoid.