3 ms·
I've started diving into Clojure...or rather drinking it's Joy from the firehose due to a birthday Barnes and Nobel gift card used for Fogus in the first editio
by brudgers 13y ago
I've started diving into Clojure...or rather drinking it's Joy from the firehose due to a birthday Barnes and Nobel gift card used for Fogus in the first edition [I'm cheap]. To me, this discussion of pure functions is distinctly not Clojuresque. Clojure is the Love Child of Common Lisp and Java™ both of which are distinctly pragmatic languages and neither of which is bogged down in a theory of pure anything.
Clojure is structured to limit mutability to allow for reasoning about state in an environment with concurrent processes, but it expects this reasoning to be done in the context of professional programming, not the ivory tower of academics. It's idea of functional programming is in the history of Common Lisp, and a good description of it is given by Graham in chapter 3 of On Lisp:
http://unintelligible.org/onlisp/onlisp.html#SEC23 http://unintelligible.org/onlisp/onlisp.html#SEC23 or http://www.paulgraham.com/onlisptext.html http://www.paulgraham.com/onlisptext.html
Essentially, the idea is to isolate mutation - just like Haskell and for exactly the same reasons but without an explicit proliferation of types.
So long as we have von Neumann machines, we're going to have mutation. The goal is to abstract as much of it away as possible to facilitate the management of state. But it never goes away, and we shouldn't imagine or try to convince ourselves that that it does...nor that it should, completely.
Polemic over; still a good article and worth a read. Certainly got me thinking.
- prospero 13y agoAll true (I gave a talk on this subject just recently [1]), but the easiest and most provable way to isolate mutation is to not use it at all. I think most people coming from an imperative background are surprised by how infrequently mutation is actually required. It's worthwhile to explore how far pure functions will take you. [1] https://www.youtube.com/watch?v=iQwQXVM6oiY https://www.youtube.com/watch?v=iQwQXVM6oiY
- brudgers 13y agoFrom farmer's pen to plate, now I know where sausage comes from. I enjoyed the presentation, thanks. While I have found it improved my understanding of functional idioms when I explained them to others, I believe I am more helpful if I point people directly toward Hickey and Graham and PLT. I can paraphrase The Value of Values but it's better watched without translation.
- nonrecursive 13y agoI think you may be misunderstanding a few things, and I hope I can clear them up :) First, the term "pure function" does not imply "ivory tower" any more than "recursion" does. Pure functions are tools that allow you to "isolate mutation". In fact, Paul Graham mentions that experienced lisp programmers "try to segregate side-effects in a few functions, allowing the greater part of the program to be written in a purely functional style" (http://unintelligible.org/onlisp/onlisp.html#SEC27 http://unintelligible.org/onlisp/onlisp.html#SEC27). So - don't let the terminology throw you :) Second, the point of the point of the chapter is to show how much can be accomplished without mutating data structures. I think that's something valuable to focus on. Saying "You can do a lot with pure functions and immutable data structures" is not the same as saying "You'll never need to mutate anything" and as the author, I apologize if my writing suggests otherwise :( Lastly, Clojure definitely places a huge emphasis on limiting mutability by using pure functions and immutable data structures. I think Rich Hickey even describes pure functions as stable bricks or stable atoms in one of his talks. My impression is that it's definitely not the love child of common lisp and java. Clojure is pragmatic, but that doesn't mean it doesn't place an emphasis on functional programming. I agree that it's "not bogged down in a theory of pure anything", but that doesn't mean it wasn't intentionally designed to support and encourage programming with pure functions and immutable data structures.
- brudgers 13y agoThe article well written. It didn't throw me or make me confused. I simply was bothered by the lack of nuance in regard to mutation. Trillions of pixels have been burned describing pure functions and giving small examples of immutability and recursion. Trees have even been killed. What makes Chapter 3 of On Lisp or SICP useful is they're Third Acts. They go deeper than pure functions. They're pivotal because that's where we start talking about the messy world where mutation is necessary. It's where we admit that there's a rationale for the way Java works even if it's implementation could stand improvement. What makes Clojure interesting is the way in which it implements that depth. Lisp went on a journey to Java and was transformed by the experience. Clojure is not just another Lisp - that side of the family has always had shared structure and a propensity for functions. There's `is` and 'testing` and agents and vars and `.` and `..`. JavaScript the good parts is a book. Java the good parts are in Clojure. Clojure is an Act V not an Act I.
- weavejester 13y agoI've been developing in Clojure for a while now, and I think your characterisation of the role of pure functions in Clojure is somewhat inaccurate. In any given Clojure code base, you'll be dealing with pure functions 95% to 100% of the time. Most of the libraries I've written are completely pure, not out of idealism, but because side-effects are simply not necessary. Yes, Clojure provides tools to limit and constrain mutability, but if you've only recently come from an imperative language, you may be surprised at how little these tools are actually used. Most of the time you're dealing almost exclusively with pure functions, so tools to manage mutability are not necessary. Discussion of pure functions is certainly idiomatic Clojure, because you'll be dealing them most of the time. Just because Clojure doesn't enforce purity through its type system, doesn't mean you shouldn't prefer pure functions the vast majority of the time.