Improving Function Definitions with Patterns

Strong typing and function overloading in Mathematica

Thomas H. Meyer

Department of Natural Resources and the Environment
University of Connecticut

Abstract

The blank operator (_) allows a function argument to be any Mathematica object whatsoever. This a major advantage of Mathematica, because it gives programmers the flexability to write functions that are practically impossible to write in other languages. However, most functions do not require such flexability; most benefit from having structure to their arguments. This structure can (i) provide argument type-checking and issue helpful error messages when a function is invoked incorrectly, (ii) be used to replace Switch- and cascading If-statements, (iii) implement user-defined data types, and (iv) implement functional overloading. This workshop will present examples of these structures, what they do, how they do it, and how to implement them. The presentation will culminate with an implementation of the law of cosines that automatically detects which version the user wants and dynamically invokes the proper form.