Now for something shorter?

Like many, this blog has been sadly neglected of late. Like many, I feel bad about it too. In my case the problem is simple: while I want to share my research notes and related material it takes a lot of time to format them so they makes sense (i.e. including the bare minimum introductory material to make it halfway self contained). And, generally speaking, I’ve lacked the discipline to do this rather than, say, trying to solve another research problem for an extra 20mins…

I’m not really sure how to solve this problem. But I’m going to try an experiment: I recently got an iPad and I’ve noticed that with it I’ve been able to make use of lots of little periods of “dead time” between meetings etc. because it is so easy to “just quickly” type an email etc.

With the wordpress app (which is sadly really buggy and keeps dumping…) I figure I can at least try and work on the blog with the iPad. Necessarily I’ll probably end up writing a lot of shorter soft posts (a format that is probably better suited to google Buzz, but no one will read that…) But at least something is better than nothing.

Anyways, to finish, I thought I’d share something I discovered today that I found really striking. A long time ago in another life I briefly worked as a research assistant in a CS dept. One of the things I was meant to do was to work with a program written in ML, a functional programming language, see e.g., this.

I was completely defeated by the task; I simply didn’t “get” functional programming, I mean, you don’t use “for” loops. Huh?!

So I’ve always maintained a healthy fear of functional programming languages since then. But then today I stumbled upon the Haskell webpage, where I found the following explanation:

Anyone who has used a spreadsheet has experience of functional programming. In a spreadsheet, one specifies the value of each cell in terms of the values of other cells. The focus is on what is to be computed, not how it should be computed. For example:

  • we do not specify the order in which the cells should be calculated – instead we take it for granted that the spreadsheet will compute cells in an order which respects their dependencies.
  • we do not tell the spreadsheet how to allocate its memory – rather, we expect it to present us with an apparently infinite plane of cells, and to allocate memory only to those cells which are actually in use.
  • for the most part, we specify the value of a cell by an expression (whose parts can be evaluated in any order), rather than by a sequence of commands which computes its value.

An interesting consequence of the spreadsheet’s unspecified order of re-calculation is that the notion of assignment is not very useful. After all, if you don’t know exactly when an assignment will happen, you can’t make much use of it! This contrasts strongly with programs in conventional languages like C, which consist essentially of a carefully-specified sequence of assignments, or Java, in which the ordering of method calls is crucial to the meaning of a program.

Now there’s an explanation that speaks to me! This one paragraph has completely unlocked the functional language thing for me! It just goes to show that often all you need is the right person to say the right thing to you at the right time.

Note: I don’t really intend to write any functional programs.


3 Responses to Now for something shorter?

  1. Sean Barrett says:

    I spent some time learning functional programming as a first year undergrad. For me the “a-ha” moment was when someone told me that, in order to write recursive functions, you need to fool yourself into thinking that the function *already exists*, and then you just call it (on a “smaller” version of the input) without worrying about the implementation. Then deal with one or two special “edge cases”, and you’re done.

    The obvious example is the factorial function, just use f(n) = n * f(n-1), then take care of the edge case f(0), and you’re finished. This is a bit too simple to really appreciate the magic, but essentially the same mental trick works on more complicated algorithms e.g. for sorting lists or various graph based problems. In those cases in can just seem really mysterious, almost as if the program just wrote itself by dint of one’s assumption that it already exists.

    • tobiasosborne says:

      Dear Sean,

      Many thanks for your comment!

      That’s a really nice way of thinking about it!

      Now. If I’m to actually do something with these new insights I guess I should actually program something… Hmm: tensor contractions maybe?

      I suppose I’m actually semi-serious: functional programming is meant to be “easy” to debug (whatever that means); if one could write a PEPS or other tensor-network contraction code in Haskell or similar then maybe this would actually be quite quick.

      (Easily 99.99% time I’ve spent coding tensor contraction routines was just debugging!)


  2. Duncan says:

    Hey Tobias! Glad you’ve discovered Haskell — I absolutely love it, though I’m not sure that I’ve fully grokked the FP philosophy yet. It just strikes me as a really beautiful language to work with.

    I do love the idea that you “specify the what not the how” (and to my naive understanding, that’s why functional languages are a la mode at the moment: if you have a good enough compiler, it can do all the nasty stuff like working out how to implement concurrency correctly for you…). I also really like the power of Haskell’s type system: it’s fun to be able to just think about what types you need, then write the most obvious function mapping type a to type b, and find that it’s what you were after!

    Hope life’s treating you well — I’m living in the UK now: will try to make it over and visit sometime soon!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: