3 ms·
> I think more and more programmers are realizing that the functional approach is almost always superior. Only for a very limited definition of "almost always"
by AnimalMuppet 12y ago
> I think more and more programmers are realizing that the functional approach is almost always superior.
Only for a very limited definition of "almost always". It's almost always superior if you don't hit performance constraints, and if your problem isn't dominated by sequencing, and if your team can handle the functional approach, and if...
Why is it superior? It controls the state space explosion, and therefore makes it easier to understand and reason about your program.
Why could performance be a problem? Think about a functional quicksort algorithm. It doesn't do mutation, so at each stage, it has to return a new sorted list rather than sorting the list in place. As the list gets longer, the performance gets worse compared to an imperative quicksort.
Now, quicksort would actually be implemented as a library, and would be tuned for speed. But the same issue comes up in other ways. If you have to transform data structures, imperative can be more efficient than functional, and the difference grows as the size of the structure increases. (I could have called this area "scaling" instead of "performance".)
When could your problem be dominated by sequencing? It often is in embedded systems, where you're sequencing external hardware to get it to do real-world things. I mean, yes, you could write all that in a monad, but if that's the dominant thing you need to control, what does it gain you?
All that said, making any individual function pure (in the FP sense) is always at least a local win, because that function becomes simpler to understand, debug, and test.
- Superpelican 12y ago>When could your problem be dominated by sequencing? It often is in embedded systems, where you're sequencing external hardware to get it to do real-world things. I mean, yes, you could write all that in a monad, but if that's the dominant thing you need to control, what does it gain you? That was exactly what I was referring to with the Arduino board example ;)
- chas 12y agoHaskell has been used to build embedded software to great effect. As you move out of the domain of simple sequencing in embedded systems and into hard realtime systems, coordinating tasks correctly becomes very difficult. Being able to track and infer information about timing and dependencies with the type system becomes extremely useful in order to make it easier. This is a very simple intro example of an DSL built in Haskell for realtime systems[0]. This is a talk on using Haskell for robotics in particular[1]. [0] https://leepike.wordpress.com/2009/05/05/an-atomic-fibonacci-server-exploring-the-atom-haskell-dsl/ https://leepike.wordpress.com/2009/05/05/an-atomic-fibonacci... [1] https://vimeo.com/77164337 https://vimeo.com/77164337
- bjourne 12y ago99% of all programming isn't cpu bound. Yes, programming to minimize your programs runtime on modern cpu:s, in contrast to keeping stuff simple, is a niche.