Functional programming is heating up. In several discussions over the blogosphere and on my blog others think I have no clue what functional programming is. After some consideration, I think they are right. I have no clue.

Well I do know what functional programming is. I’ve used it myself. But I have no clue how to do a business application in FP. Others don’t know either. They talk about Bezier curves, square roots, fix points, y-combinators, power functions or remodel FP into OO and think that’s still FP. Weird.

Suppose I need to write an application. There are customers who can buy products. Based on the customer and the products he bought he can buy other products. Those products he books do cost him money which he needs to pay to the company. I’m quite sure how to design such an application in OO, discuss it with some domain experts and write the code. I also know how to write such an application in a way that other developers in 5 years from now can read, undestand and extend the code.

But how would I write such an application in Lisp? Or Arc? Teach me.


12 Responses to “Show me functional programming, I have no clue (obviously)”  

  1. Gravatar Icon 1 foo

    Read the book ‘Practical Common Lisp’ by Peter Seibel, for a start. Available online and printed

  2. Gravatar Icon 2 stephan

    I’m not sure that you really mean this book:

    http://www.gigamonkeys.com/book/

    where the most complex example is

    “Practical: An MP3 Database”

    Peace
    -stephan

  3. Gravatar Icon 3 Doug Pardee

    ACID becomes basic when CRUD is reduced to just R.

    Application functionality tends to suffer a mite, though.

  4. Gravatar Icon 4 stephan

    @Doug: As always imagine I have no clue what you are talking about. From my experience it’s always best if people assume no clues on my side.

    But I do like R and K as programming languages. C and A not so much. E was funny but didn’t make it. And D?

    Lately I asked a candidate about ACID in an interview. It’s funny when people want to use transaction for a R-only application. CRUD seams a non-issue nowadays.

  5. Gravatar Icon 5 James Iry

    Doug was making a joke based on the “purely functional” meaning of functional programming - i.e. programming without side effects. If you strip out all but the R(read) in CRUD (create read update delete) then purely functional is pretty easy, but the application might suffer. Of course, he knows that’s a joke, there’s no conceptual reason it couldn’t be done in Haskell (see http://www.haskell.org/haskellDB/ for a proof of concept, though definitely not ready for prime time, example).

    Anyway, it’s irrelevant since you asked about Lisp, by which people usually mean Common Lisp, which is an impure language (it allows arbitrary side effects). Importantly it has both OO and functional features. Let me turn this around, how would YOU write an enterprise app in language with both OO and functional features? What if you had the ability to extend the language’s syntax with your own syntax? What if there was an implementation (ABCL) on the JVM so you could even use all your favorite libraries? I know you’re going to object that it’s no longer FP at that point - but there’s no reason you can’t throw as much FP into the mix as you want, by whatever your definition.

    How would it be done in Arc? Who knows, it just got released today. Nobody knows anything about it except PG and a few hard core fans.

    Also, I want to re-recommend Practical Common Lisp. Yeah he doesn’t explain exactly your domain, but the general idea of using syntactic abstraction (macros) to create your own DSL (domain specific language) applies across all domains.

  6. Gravatar Icon 6 stephan

    “Let me turn this around, how would YOU write an enterprise app in language with both OO and functional features?”

    As written in the other thread, lots of my OO code is side effect free, immutable and doesn’t come with state. In Java.

    “What if you had the ability to extend the language’s syntax with your own syntax? ”

    Won’t use, language design is hard, most definitely I’d screw up and the developers who need to maintain the code after I left would be screwed.

    Do I use map , filter, reduce, each? Mostly not, see the reasons we discussed already. Most often that’s not needed. When it does make sense I use a

    List persons = select(person, ageSelector);

    I have the book for some years (thanks dostoyevsky ;-), it didn’t convince me back then. But because you re-recommend it to help my enlightenment, I’ll take another look.

  7. Gravatar Icon 7 James Iry

    > As written in the other thread, lots of my OO code is side effect free, immutable and
    > doesn’t come with state. In Java.

    Good. Then you’ll love Scala because its core semantics are basically Java’s but it makes immutable, side effect free code much easier to write. For instance, it’s for loop isn’t really a for loop and can used entirely side effect free - even without Iterator (instances of which must be stateful).

    > Won’t use, language design is hard, most definitely I’d screw up and the
    > developers who need to maintain the code after I left would be screwed.

    Then I can say that Lisp is not your language. If you don’t use macros then you’d be paying a syntactic price for an unused feature. You’d be better off with other functional languages without syntactic abstraction or where the syntax hasn’t been kept flat to support it.

    > Do I use map , filter, reduce, each? Mostly not, see the reasons we discussed
    > already. Most often that’s not needed. When it does make sense I use a
    > List persons = select(person, ageSelector);

    I’m assuming that person is also a list. In Scala you could write exactly that or write “person filter {_.age > 3}” The semantics of the later are in fact essentially the same as the former, you just don’t need to write an anonymous class by hand. In Scala, this

    persons = person filter {_.age > 3}

    Looks a lot like your “$it.age > 3″ code eh? But it’s shorthand for the Scala equivalent of this Java

    List persons = person.filter(new Function1() {
    public Boolean apply(Person person) {
    return person.age > 3;
    }
    }

    Nifty eh? It’s OO, it’s FP. It’s both. And that Java is the same as this Scala

    persons:List = person.filter(new Function1[Person, Boolean]() {
    def apply(Person person):Boolean = {
    person.age > 3;
    }
    }

    Pretty much like the Java version - just some minor syntactic variation e.g. symbol:type instead of type symbol, square brackets instead of angled brackets, and a keyword (def) for indicating the start of a method.

    So without changing semantics a bit, the Scala shorthand clears away the cruft of the anonymous class and reveals the core concept - finding people where the age > 3.

    > I have the book for some years (thanks dostoyevsky ;-), it didn’t convince me
    > back then. But because you re-recommend it to help my enlightenment, I’ll take
    > another look.

    It’s a good book. I also liked Graham’s On Lisp (I know you’re know PG fan, and I don’t always agree with him, but the man really can write).

    But I’m getting the impression that you equate Lisp with functional programming. Common Lisp (and other lisps) are in the very broad functional family, but there are many interesting languages in that family. I’m most familiar with Scheme, Haskell, and Scala - and of those, only one is a lisp. Various ML’s are popular in the functional crowd, especially OCaml - and F# is a variation on OCaml. There are very researchy languages like Coq and Epigram.

    So if you don’t like Lisp, that’s fine. You’re missing out, IMHO, on something deeply beautiful there, but what you’re missing out on is syntactic abstraction and a “code is data mindset” not functional programming.

    Functional programming can be done in languages with richer syntax and powerful static typing systems - you don’t need to throw out the functional baby because you don’t like the Lisp bathwater. And if you like OO, then you don’t need to abandon it either.

  8. Gravatar Icon 8 stephan

    Well I’m interested in Scala, and I might use it for development, but gaining the functional power I lose hundreds of thousands of developers (for an open source project) or dozens of developers (commercial project). Not sure if { _.age > 3 } is worth it compared to inner classes.

    Do you know a JVM language which resembles bla?

  9. Gravatar Icon 9 James Iry

    > but gaining the functional power I lose hundreds of thousands of developers

    Of course. And when Java was new you could have said the same thing about it compared with C - “by gaining cross platform dynamic linking, multi-threading, and garbage collection I’m losing developers.” There are trade-offs in being an early adopter. I can’t argue with that.

    > Not sure if { _.age > 3 } is worth it compared to inner classes.

    If that was all that Scala had then it wouldn’t be worth considering. Groovy has that and so much more in the form of dynamic metagprogramming and “builders” and the nullsafe dot operator and…

    But Scala has unique things to offer of its own: higher kinded types, explicit self types, type inference, type members, existential types, etc. If you like static typing, Scala brings OO type systems up to speed with some of the more powerful type systems out there. It also has pattern matching, optional laziness, the aforementioned “for” which is crazy powerful and quite clean, etc, etc.

    Now, I happen to like Scala, but I don’t want to stifle your exploration into other languages. Groovy is a very easy one to noodle around with and dip your toe into the interplay of functional and OO features and, as I said, dynamic metaprogramming changes the game substantially.

    If you don’t insist on staying on the JVM platform, F# is one of the few modern functional languages with the kind of IDE and other tooling experience that you’ve come to expect in Java land. Scala and Groovy are both trying to catch up, but they don’t have Microsoft $ behind them.

  10. Gravatar Icon 10 stephan

    Oh, I’ve been using Groovy for years, and Grails since 0.1 I guess ;-) See some old posts on this blog (and perhaps some old posts on the IntelliJ forum with requests for Groovy support).

  11. Gravatar Icon 11 Mondragon

    Shut the fuck up and go back to yer cage, Java database code monkey.
    Nuff said.

  12. Gravatar Icon 12 stephan

    Ah. What insight.

Leave a Reply



RSS