Live data from Hacker News

Disadvantages of purely functional programming

flyingfrogblog.blogspot.com

31–40 of 70 posts

Re: Disadvantages of purely functional programming

#31
post #23

Good points, although I agree that a lot of them boil down to similar statements. I have settled on a style of doing "functional-like" programming in Python and C++. It's more about the high level architecture than low level coding details. It means being very paranoid and rigorous about state -- but still having great tools to express it! For example: Using ZERO mutable globals, and passing state in as a parameter t…

Interesting points.

As a matter of style, I still prefer using currying over "one trick pony" (single method/function) classes, though. Sometimes this means I have several "layers" of references to the original function, with varying numbers of parameters applied as each layer further specializes (adds configuration). I'm doing most of this type of work in Javascript, though, so YMMV.

(PS - also did some work with Lisp in Uni back in the 80s, and sundry imperative/oop/func ; static/dynamic things since)

Re: Disadvantages of purely functional programming

#32

This post lists 9 points, but the first 7 are all variations of "mutable data structures suck in a pure language!" and the other 2 are "look at all these fucking idiots who talk about purely functional programming!" So the first 7 points are true because pure functional programming, by definition does not support mutable data structures very well. It is what it says on the tin. If you go on stack overflow you'll find…

At least point 4 seems to be about algorithms, not data structures? That is, there's nothing up front that says graph algorithms must be faster using mutable data structures, but that is apparently the case, so far.

Any imperative algorithm / data-structure, e.g. union-find, can be implemented purely by using an IntMap for memory and a State monad, with roughly equivalent performance: https://hackage.haskell.org/package/union-find

As to whether that actually counts as "purely functional programming", I can't say. Honestly the whole term seems quite misleading. https://chadaustin.me/2015/09/haskell-is-not-a-purely-functi...

Re: Disadvantages of purely functional programming

#33
It seems to me that yes, you need an "escape hatch" in an FP language to make your updates.

It would be nice if the language required such functions, and the modules in which they reside, to be flagged. (I don't know if Haskell does something like this with mutation, or not)

It also seems that an "actor model" would be a good way to encapsulate the updates in an otherwise FP program by having a loop/reduce/fold wrapped around the mutable data responding to request-events and generating responses. This allows the other pure/immutable/idempotent type of code to remain isolated from it.

Re: Disadvantages of purely functional programming

#34

I found another article linked from the one linked here more interesting, but it's not about "FP" but only about Haskell: "Why is Haskell used so little in the industry?" - http://flyingfrogblog.blogspot.de/2010/05/why-is-haskell-use... Go and grab some popcorn before you move on to the comment section... It's from 2010, I'd be interested in an update, just out of mild curiosity.

The article wasn't particularly good, even in 2010 ...

Re: Disadvantages of purely functional programming

#35

Sadly, the author's chosen style is a rant and it is counterproductive. There's plenty of words and claims are made but benchmarks or code are nowhere to be seen. Why should anyone take these claims at face value? > Furthermore, most functional programming languages (OCaml, Haskell, Scala) are incapable of expressing a fast generic mutable hash table because they lack the killer combo of: reified generics, value type…

It's also setting a very high bar for functional languages to clear. Off the top of my head I can't even name a garbage collected language that has reified generics and value types.

Re: Disadvantages of purely functional programming

#36

Sadly, the author's chosen style is a rant and it is counterproductive. There's plenty of words and claims are made but benchmarks or code are nowhere to be seen. Why should anyone take these claims at face value? > Furthermore, most functional programming languages (OCaml, Haskell, Scala) are incapable of expressing a fast generic mutable hash table because they lack the killer combo of: reified generics, value type…

It's also setting a very high bar for functional languages to clear. Off the top of my head I can't even name a garbage collected language that has reified generics and value types.

> Off the top of my head I can't even name a garbage collected language that has reified generics and value types.

C#

Re: Disadvantages of purely functional programming

#37
post #2

I don't get why people would use functional programming for anything. It's both more difficult and slower.

A significant part of my work is in a technical/numerical computing domain where Fortran is still prevalent. Most students who now enter the field have a tendency to start with Matlab, or feel like they are doing something truly modern if they use C++.

For various reasons, I chose to use OCaml, and from my experience, the difficulty part is only true insofar as the learning curve goes. Once past that, FP-style problem-solving is spectacular in that the vast majority of time you spend goes not into the coding, but rather (a) thinking about what exactly your problem is, and (b) how to decompose and solve it. I can't tell you how many times in Matlab, people start writing code before they've even fleshed out (a) and (b), simply because there's so much drivel and overhead that one naturally tends to feel they're "getting things done" by jumping in and writing this code.

Once I had a sufficient grasp of OCaml concepts and syntax, I would find myself getting frustrated by how little code I might get done in a day. Upon stepping back and reflecting, that was because I had not worked my way through (a) and (b), and that is where the human thinking is really required. I have since been delighted at the utility of each line of code I write and am similarly annoyed by the low signal-to-noise of something like Matlab.

With respect to slower, I think my points above address "slowness" in terms of development time. In terms of runtime, it's very much a function of your choice of language/ecosystem and paradigms. If GC and typical functional overhead are a constraint in your problem domain, one can always bring over a functional style into modern languages like Rust, where performance and lack of GC are first-order design goals. But you can't tar all of FP with the label of "slow" any more than anyone can make a blanket statement that "solving matrix problems is slow". It just doesn't make sense.

Lastly, as others have pointed out, Jon Harrop's points refer to purely functional styles and languages. In the end, striving for functional purity may not make sense. To illustrate, one can easily drop into an imperative style where needed in OCaml, for example when doing I/O. I use this quite a bit. But make no mistake that adopting a functional approach where it makes sense can pay dividends.

Re: Disadvantages of purely functional programming

#39

Sadly, the author's chosen style is a rant and it is counterproductive. There's plenty of words and claims are made but benchmarks or code are nowhere to be seen. Why should anyone take these claims at face value? > Furthermore, most functional programming languages (OCaml, Haskell, Scala) are incapable of expressing a fast generic mutable hash table because they lack the killer combo of: reified generics, value type…

> there was never a situation when I said to myself "if only Data.HashMap (unordered-containers) was faster"

Here's a Haskell programmer looking for a faster hashmap: https://github.com/ndmitchell/shake/issues/418

Shake in fact does use reified generics (a.k.a. Dynamic/Typeable) and a Value type, and recommends disabling idle GC. I'm not sure if there's a "GC write barrier", Shake just uses MVar locks.

Re: Disadvantages of purely functional programming

#40

Earlier quoted context omitted.

> Huge body of problems can be handled by simple recursion but I've yet to see non-functional programmers use it. I don't know why. Stack overflows.

Tail-call optimization eliminates those. Assuming your language provides it, and assuming you trigger it correctly. ;) http://stackoverflow.com/questions/32164370/does-elixir-infi...

TCO eliminates some types of recursion. Not all of them.
Post reply on HN