Category Archives: Functional programming

Tagore Smith on functional programming

My travails with functional programming have been a bit of a recurring theme on this blog, and I have to admit that my attempt to learn Scheme has stalled, more than anything due to all the other things I’ve been doing.  I’m sufficiently aware of it to feel guilty, but not sufficiently to actually invest the time I ought to into actually learning it.

But today, Togore Smith wrote a brilliantly insightful comment on one of my oldest posts (Closures, finally explained!), and it’s got me thinking about this subject all over again.

Continue reading

Five and a half months late

If only the XKCD guy had published this comic five and half months earlier, I could have used it to illustrate my functional-vs.-imperative-programming article.

C’est la vie.

Unrelated addendum

Am I the only person in the whole world who finds this joke funny?  “A man walked into a pub.  He said ‘Ouch!’.  It was an iron pub.”  It makes me laugh even to type it here, but so far not one single person I’ve told it to has laughed.

Also: “1st man: My wife’s gone to Jakarta.  //  2nd man: Of her own accord?”

How slow are functional implementations of quicksort?

I’ve been talking with my colleague Jakub Skoczen.  He’s insisting that functional implementations of quicksort, which make and return new arrays at each level of recursion, must surely be much slower than imperative implementations that modify an array in place.

And I can see his point.

Continue reading

The long-overdue serious attempt at Lisp, part 2: is Lisp just too hard?

First, thanks to all of you who advised me on what Lisp to learn.  I found it really helpful to get so many different perspectives, and I hope those of you who advocated Common Lisp or Clojure will not be offended that in the end I chose Scheme.  (Actually, I bet you won’t be: one of the pleasant aspects of the comments on that article was the consistent sense that all the comments were of the form “I prefer this Lisp, but if you learn any of them that’ll be great.”)

A few people suggested that I should learn all the different Lisps rather than just picking one.  As a looong-term project, I guess I agree with that; but here and now, I need to pick one and run with it: and Scheme it is, for its minimality, elegance and purity.  I figured that if I learn Scheme it’ll be easier to move from that to Common Lisp later rather than vice versa.  (I was tempted by Clojure, too, not least because there is only one of it, but it feels just a bit too new-fangled to be a good starting point.)

So two days ago I started working my way through R. Kent Dybvig’s highly regarded book The Scheme Programming Language [,].

Continue reading

The long-overdue serious attempt at Lisp, part 1: which Lisp?

This is a blog about being a reinvigorated programmer.  So it’s ironic that the most successful articles so far (at least in terms of number of hits) have been about The Good Old Days — Whatever happened to programming? and Programming the Commodore 64 being two examples.

One possible response to this would be to change the blog title to The Nostalgic Programmer, but I’m not going to do that — despite what you might think from what I’ve been writing, I am actually looking forwards more than backwards, and there are plenty of things I am excited about right now, including Ruby, refactoring, REST, Rails and even some things that don’t begin with R.  Lisp, for example (although I guess I could have squeezed that into the R-list by substituting “recursion-based languages” or somesuch).

Continue reading

The difference between imperative and functional programming

Have you ever had this frustrating experience?  You need to drive somewhere that you’ve not been before, so you look that place up on the web.  You find its site, and start looking through the pages for the address and postcode.  Instead, you find this kind of thing:

To get to us from the south/east (for example, if driving from London), the easiest way is to get onto the A4136 heading southwest, then turn right onto Morse Road which takes you straight to Ruardean.  Our house is on the right as you approach from this direction, almost immediately after the 30-mile-per-hour speed-limit sign that marks the boundary of the village.

You didn’t want instructions on how to find the place.  You just wanted to know what the address is: you know how to reach a place with a known address, because you have Google Maps and a satellite navigation system and a helpful wife who will shout “Left!  Left!  No, LEFT!  Why didn’t you turn LEFT?” when required.

The postcode GL17 9XF tells you what the destination is; the series of instructions on the unhelpful web-site tell you how to reach the destination.

Continue reading

Closures, finally explained!

Programming language weirdos (by which I mostly mean people who program in Lisp) are forever telling you that you need closures, and that your language is unbearably primitive if it doesn’t support them.  Turns out that they’re right, but their rightness is completely obscured by the horrible, uninformative term “closure”, and even more so by the fact that the definition given for the term is usually some opaque hunk of verbiage like “a first-class function with free variables that are bound in the lexical environment” (from the Wikipedia article on the subject.)

It also doesn’t help that the examples people usually give are so lame: it’s all “Here’s a function that returns a function that yields the value of a hidden variables that is incremented every time it’s called”.  Whenever I see examples like that, I just think “Why would I ever want such a thing”?

Continue reading