Posts Tagged: First Class Functions


19
Jul 12

Functional Programming is Dead, Long Live Expression-Oriented Programming

It’s funny how over time the meaning of a technical word will converge to something halfway between what the experts intended and some fuzzy notion consisting of the most easily graspable components of that idea. In this inevitable process an idea is stripped of all of its flavor and is reduced to a set of bullet points graspable in an hour long presentation. Over the last few years this has happened to functional programming, right along with its popularization.

From Wikipedia:

  • First-class and higher-order functions
  • Pure functions
  • Recursion

Now that almost every language has tacked-on “functional features”, the functional party is over. The term has become just as perverted as Object-Oriented is to its original idea. It seems as though these days all it takes is lambda expressions and a higher order functions library to claim your language supports functional programming. Most of these languages don’t even bother to include any kind of proper support for simple tail recursion, much less efficient co-recursion or function composition. Oh, and any kind of inclination toward even encouraging purity? You wish.

But this isn’t necessarily a bad thing. The term functional isn’t at all evocative of the actual properties that make functional languages so wonderful. The term we should have been using all along is Expression-Oriented Programming. It’s the composition of expressions, the building of programs by sticking together little modular pieces, that makes functional languages great and first class functions are just a small part of enabling that. Expression-Oriented Programming tends towards first classing everything.

However, even the term first class is too weak to pin down this concept. All first class means is “as good as most other things” and this can still imply a really awful lowest common denominator. Just take a look at Microsoft’s C#. Sure, functions are first class, but it’s still a pathetic attempt at emulating what is possible in functional programming languages because the rest of the language isn’t.

Let’s end with a simple example to drive home the point. In C#, because the switch statement doesn’t produce an expression, you can’t assign its result to a variable. You even get an error that tells you so.

However, F# does a much better job of supporting Expression-Oriented Programming as almost every language construct outside of type definitions is itself an expression.

Expression-Oriented programming a simple idea, just programming with little composable parts, but it leads to beautiful and expressive code. It is at the core of why programs in functional languages are small, simple and less error prone. We should give credit where the credit is due, not to just the functions who are but one small player in the Expression-Oriented story.


21
Jun 10

Love the Lambda

Just this past Saturday I gave a completely new talk at the third Code Camp Hartford.  This talk was inspired by a previous mid-talk realization: most C# programmers (and almost the entirety of my audience) have never written a single lambda expression or anonymous delegate.  From this I further realized that I’d never be able to teach F# to those who are completely unfamiliar with the concept of functions as first class language constructs.  First, this idea and its ramifications would have to be taught.

lovethelambda

So the idea for Love the Lambda was born:  I would demonstrate both the usefulness and sheer novelty of first class functions and do so simultaneously in C# and F#.  More than that, I would use the opportunity as a kind of F# omnibus.

Here was the general game plan (as written by me imagining the thoughts of some intelligent, but yet uninformed, person sitting in the audience):

  1. First class functions sure are great!
    1. Oh man, and with closures they are even better!
    2. I’m going to use this stuff in C# all of the time!
    3. Wow, that F# code looks so much cleaner than the C#!
  2. Now check out what you can do with partial application!
    1. Oh wow, this pipelining stuff puts LINQ to shame…
    2. …and function composition is amazing!
    3. Hey, wait a minute, you can’t do this in C#…. :(
  3. Good thing F# integrates really nicely with C#.  I can pull it right into our existing projects!
    1. Plus F# has tons of other amazing stuff, I’m going to start playing with it tomorrow!
    2. I can’t believe how much less code I need to write when I use these features!
    3. F#, where have you been all my life?!

All that said, the first iteration of this talk worked out a bit different than my initial vision.  This was mostly due to running out of time when constructing the presentation.  I still think the ideas I was trying to convey are in there but not as sharp and as easy to grasp as I’d like.

Still,  the attendees seemed really happy with the talk and the review forms agreed.  Everyone left my talk loving the lambda at least a little bit more.

If you are interested, I’d encourage you to download my slides and code samples.  Also, I’m always interested in hearing about how I might improve my talks, or further spread my love of functional programming.  Please don’t hesitate to contact me if you want to share ideas.