Garbage Collection and Functional Programming

Posted by on in Blogs
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.
Comments are not available for public users. Please login first to view / add comments.