1

I have started experimenting with Clojure, and I am impressed by what it can do. However, I don't think I have really 'got' functional programming design. My background is in Smalltalk, and that colours how I think about design. I would like to really understand how FP differs from OO, and one approach would be to have a good side-by-side comparison of a solution.

I don't think the problem needs to be that complex. Maybe it is would be enough to show how FP would implement the typical bank account problem?

Does anyone know of a suitable resource?

Andy Burnett
  • 305
  • 1
  • 5
  • 5
    @VitalyIsaev: [Python is not a functional language](http://stackoverflow.com/questions/1017621/why-isnt-python-very-good-for-functional-programming) – Chuck Apr 23 '14 at 18:14
  • @Chuck, did I claimed that? – Vitaly Isaev Apr 23 '14 at 18:18
  • 5
    @VitalyIsaev: Yes, you suggested studying Python to learn about functional programming and said that "it uses both paradigms." In fact Python does not have very good support for many aspects of functional programming. Python's philosophy is that there is one "best" way to do things, and Guido's preferred approach is imperative. It is a stretch to say Python "uses the functional paradigm" unless you just mean "Python has functions." – Chuck Apr 23 '14 at 18:21
  • 2
    Why don't you provide a _short_ example in Smalltalk of a problem solved with OO paradigm and an _attempt_ at a Clojure solution, and I'm sure one or more Clojure tag followers will answer with suggestions. Note that Clojure is a very practical language, so while it has a purely functional subset, it is not constrained to pure FP. So, if you say "bank accounts", the Clojurians are probably already thinking [transactions with refs](http://clojure.org/refs) rather than purely functional. – A. Webb Apr 23 '14 at 18:39
  • I like the idea of offering a Smalltalk solution and letting Clojure experts show how they would have tackled it. I shall go off and write something and then submit it as a new question. – Andy Burnett Apr 23 '14 at 23:12
  • @AndyBurnett When you do, please do include at least an _attempted_ Clojure translation of your own so that your questions and the answers can be focused. – A. Webb Apr 24 '14 at 00:28

2 Answers2

1

I think that most books on Clojure will have something like a simple bank account program, which I view as a little database in which states get updated. For example, Halloway and Bedra's Programming Clojure has a program that keeps track of music recordings. When you read through a description of that program, you'll immediately see how to map it into an OO solution. However, I don't think you're going to "get" FP from such a simple example. Programming Clojure also has functional snake game. You could compare that to an OO snake game. (I'm not promoting Programming Clojure; it's a worthwhile book but I think it has a number of flaws. It just happens to be the Clojure book that I know best. A lot of people seem to like it, though.)

I'd suggest another strategy, maybe as a supplement to yours, for grokking FP: Read something introductory that immerses you in an FP worldview. My favorite is Friedman and Felleisen's The Little Schemer. It uses Scheme, not Clojure, but the ideas translate. There is a Clojure website devoted to it, but I recommend working through the original book in the way that it recommends, covering parts of the page as you go. I think the book that gave me an understanding of parts of FP that go beyond The Little Schemer, extending to much of what you find in the Clojure world, was Bird's Introduction to Functional Programming using Haskell. I feel funny suggesting a Haskell book, though. Why not a Clojure book? Well, maybe one of the Clojure books will have the same effect of giving you a pure baptism in the FP Way of Thinking, but the Clojure books I've read (P.C, The Joy of Clojure), though very good books, are somehow less immersive--maybe because they're more practical. On the other hand, P.C (and probably some other Clojure books) is (are) largely targeted toward OO programmers, so that might actually be what you want.

Mars
  • 8,689
  • 2
  • 42
  • 70
  • Thanks, that is a very comprehensive list of options. I will go and try your suggestion. – Andy Burnett Apr 23 '14 at 23:13
  • On a very similar note, I think [Learn You a Haskell](http://www.learnyouahaskell.com) is an excellent introduction to FP, and it's also easy to get into if you're used to OO programming. Although Haskell and Clojure have numerous differences, what the two languages have in common is a solid foundation in FP. – Dave Yarwood Apr 24 '14 at 02:22
0

A few random observations on Clojure from a Smalltalker's perspective.

  • The Clojure data structures will remind you of Smalltalk's collection classes, but immutability/persistence takes a bit of getting used to.
  • You already use do and its relatives to abbreviate control structure. You'll find that the sequence library (map, reduce, and the rest), with laziness offer you another level of power and concision.
  • You're already used to writing your code in little pieces. The pieces will get smaller and there will be more of them.

But

  • Clojure is less pure than Smalltalk: not everything is an object.
  • Nothing like the wonderful Smalltalk browser is standard.

If you like Smalltalk, you'll love Clojure.

Thumbnail
  • 13,293
  • 2
  • 29
  • 37
  • Thanks, those are really helpful observations. I think the fact that elements of clojure look so similar to Smalltalk is part of what I am struggling with. – Andy Burnett Apr 30 '14 at 12:20