Skip to content

Garbage Collection and Functional Programming

This post is going to be short and sweet, because the point is very simple: If you use a functional programming language (and, if you want to learn to think outside of the Delphi box, you should), then you will be using garbage collection.

There are many reasons why this is the case. Garbage collection is a core feature of functional programming, because functional programming features do not work well with the semantics of explicit memory release. It is telling that language implementors have found it easier and more correct to create real-time garbage collectors than to create and work with non-garbage-collected variants of functional languages.

I was unable to find a single example of a truly functional language which did not use garbage collection in any standard version. Haskell, OCaml, Common Lisp, Erlang, ML, Scheme, etc., are all garbage collected. Moreover, functional features are commonly added to languages like C#, Ruby, Python, ECMAScript, and others, which weren’t designed as functional languages, but happen to be garbage collected. Whereas languages like Delphi/Win32 get fewer such features, despite their continuing evolution. The FC++ library, which adds some functional programming features to C++, uses a reference-counting method of garbage collection internally, to its great benefit. The authors note:

For instance, compared to Läufer’s approach, we achieve an equally safe but more efficient implementation of the basic framework for higher order functions. This is done by allowing function objects to be multiply referenced (aliased), albeit only through garbage collected “pointers”. The difference in performance is substantial: compared to Läufer’s framework, and running with the same client code (the main example implemented by Läufer) we achieve a 4- to 8-fold speedup.

The D programming language is designed to combine the power of C++ with the productivity and contemporary features of Ruby Python. It supports function literals, closures, and lazy evaluation. It doesn’t use a runtime, or a JIT compiler, but it uses garbage collection for heap management.

{ 2 } Comments

  1. Jolyon Smith | January 24, 2008 at 3:10 pm | Permalink

    It’s one thing to talk about languages, it’s quite another to talk about applications built using those languages.

    It may be telling that Functional Languages need garbage collection. It is also telling that I don’t know of a single non-trivial desktop application written using a functional language (and I don’t know anyone who does, either).

    Great stuff for the classroom though.

    ;)

  2. Craig | January 24, 2008 at 5:36 pm | Permalink

    You are right Craig S. It reminds me of 1997 when desktop developers dismissed web developers as script kiddies not real developers. There was some truth to that back then but these days that kind of thinking is backward say the least.

Post a Comment

Your email is never published nor shared. Required fields are marked *

Bad Behavior has blocked 713 access attempts in the last 7 days.

Close