7 ms·
It's not that I don't think that programming in a functional style isn't a good idea , it's just that the discussions about functional programming and the langu
by brudgers 12y ago
It's not that I don't think that programming in a functional style isn't a good idea , it's just that the discussions about functional programming and the languages people use to implement its style so often suggest:
If you use language 'x', then using
mutation is wrong.
and recently, I've been thinking about the practically important but socially awkward question:
What language is best for implementing
mutable state?
By which I don't mean:
What language makes avoiding mutable
state impossible?
because given my skill set on some absolute scale that encompasses Knuth, I've got big fish to fry. Trying to maintain ideological purity is a distraction at best and an impediment at worst, if I am ever to see an [m42] and have a solid intuition about its solution.
Even the most rousing chorus of 'Onward Christian Soldiers' isn't going to help me implement Hoare's Quicksort in Clojure. Consing and filtering are great, but they miss the idea of working in-place. Racket's built in O(log n)priority queue isn't a substitute for a traditional O(1) queue implementation:
+-------+
| | o-|------------------+
+-|-----+ |
| |
v v
+-------+ +-------+ +--------+
| | o-|--->| |o-|--->| | nil|
+-|-----+ +-|-----+ +-|------+
| | |
v v v
'a 'b 'c
The built in priority queue is mutable anyway.
The illustration of queues in Racket isn't rendered from whole cloth. If one searches the Racket documentation (depth first is implied?) the only hit for 'queue is the priority queue which lives in the 'data library [and again, it's still mutable and not thread safe]. But the real problem is that providing Racket implementations on Rosetta code is one of those ways Racketeers are encouraged to give back, and the implementation of LIFO on Rosetta Code shows the priority queue, rather than an actual queue, in part because using 'mcons is considered taboo under functional programming mores. It's a socially acceptable answer, rather than a correct one.
Some things just have to be to be mutable in order to get built in a way that will be useful when dealing with large tables when the first test of usability is getting built in the first place. It's not that playing with dangerous objects ought to be encouraged, it's that it there's nothing wrong with a little dynamite now and then for removing stumps when the alternative is lifting the grinder to the top of the cliff with a crane. All that really matters is acting in accord with the fact that, yes, we're using dynamite.
There's a continuum between Coq and MMIX. A good functional programming language allows refactoring a solution from right to left. It doesn't pretend as if there isn't a right wing or that it doesn't matter politically.
- taeric 12y agoHaving finally decided to read Knuth's main works, I have to confess I am being drawn more and more heavily into this way of seeing things. The beauty of how Dancing Links works is rather remarkable. Similarly, finally understanding how to read some of the algorithms as specified has helped to see just how "imperative" algorithms can be analyzed rather comprehensively. Also, as a major aside, I would encourage more folks to try the Knuth works. Definitely feel free to skim the math, as that is some pretty hairy stuff. At the same time, keep trying to go back to it later. Most of it isn't undoable, so much as it is just completely foreign to what you probably do day to day. Seeing the connection between it and many typical programming tasks can be tough. (Indeed, I don't think I've done so, yet.)
- Jtsummers 12y agoI assume by Knuth's main works you're meaning his The Art of Computer Programming. If you've got a decent math background (college level with some discrete math and calculus, it will still be pretty difficult for solo study by someone with just a high school math background) and find the stuff in Volume 1 to be a bit too difficult, I recommend Concrete Mathematics. It's essentially the math portions of Chapter 1 extended to a full text. I found it much easier to get into. Some portions are essentially calculus for discrete functions. Reading it made a lot of the calculus things that I had difficulty with suddenly click (I passed Calculus II by willpower, not understanding).
- taeric 12y ago:) I did indeed mean that. And, oddly enough, I did just pick up Concrete Mathematics. I am just close enough to "get" much of the math. What I can't do is make some of those leaps myself. Worse, I'm not entirely clear how they relate to what I do, day to day. Despite all of that. I am loving every moment of these books. Highly highly recommended.
- sp332 12y agoYou could look at Rust? It has mutable state but strict ownership which is enforced at compile time.