Hacker News new | ask | show | jobs
by nerdtime 2070 days ago
Functional programs tend to be more modular than imperative or OOP counter parts but nobody really knows why nor do they understand the cases where FP becomes less modular.

FP is only modular when you use combinators. If you use closures then it's no longer modular.

   f x y = x + y
   g y = y * 2
   w x = (f x) . g
g and f are combinators and modular and w is the composition of both of those combinators.

   w = \x -> (\y -> (x + y) * 2)
In this case the above is not modular because it doesn't use combinators. The above style is actually kind of promoted by haskell when you need to do things with side effects. It actually makes FP more complex than it needs to be without improving modularity.
1 comments

Huh, what do you mean by "modular" that isn't satisfied by the closures?
Both examples have multiple functions defined as the definition of w.

But in one example f and g can be reused in other contexts, in the other example the two functions are tied together by free variables. Haskell heavily promotes the latter style with do notation. Any function written in the first style is decomposable into component combinators, any function written in the latter style cannot be decomposed.