Functional Pattern Matching

Summary: Functional Pattern Matching in Squeak
Author: Lukas Renggli
Owner: Lukas Renggli (lr)
Co-maintainers: <None>
PackageInfo name: <Not entered>
RSS feed:


Most functional programming languages like Haskell or ML allow to define functions using pattern matching. Instead of testing arguments in the body of a message, this allows to specify case-based function definitions in a very elegant way. An example of such a definition would be the factorial:

fac(0) = 1
fac(n) = n * fac(n - 1)

This package provides an object model to describe such functions, a parser to read the syntax, visitors to transform the code to ordinary smalltalk code and a browser to make it easy to work with. The advantage is, that it is fully integrated into Squeak and allows to be used with any other part of the system. Furthermore it is transparent to any source-management tool and does not require a runtime library.

The goal of this package is to allow the developer to write functions in a more natural way and to transform these functions to ordinary Smalltalk code: The previous example can be written and compiled in the functional browser using a slightly enhanced Smalltalk syntax, as seen below. Note that in this example the order in which you specify your functions is important:


^n * (n - 1) fac

The special thing about this is that the receiver of the message #fac is used to do the pattern matching. In real functional languages there is no concept of receiver, so purists might think that the receiver is just the first argument passed to the function and is matched as any other argument. Of course, if all the functions specified by the programmer fail to match, #doesNotUnderstand: is evaluated.