Summary
Most of the programs we have created in the first five chapters are relatively short and compact, focused on solving problems that are quite manageable. These pieces of code are often sufficient when working on a problem and you are looking for a quick result. But before long, something more is needed: a series of computations that have to be repeated perhaps, with slight variations each time; or you need to share your code with colleagues or students; or perhaps you are developing an application for a client who knows little of the Mathematica environment or language and is looking for a solution that integrates with what they are already comfortable with.
Whether you are writing code for yourself or others, it is important to develop programs that are not dependent upon the current state of the user's Mathematica session: previously defined symbols should not interfere with code in any way; side-effects of an evaluation should not cause surprises; programs should respond gracefully when things go wrong. All modern programming languages have frameworks designed to deal with these things, and Mathematica is no exception. This chapter introduces these structures and incorporates them throughout the rest of the book.
The first construct we will look at is localization of variables. This is a mechanism to isolate symbols and definitions that are local to a function in order to keep them from interfering with, or being interfered by, global symbols.
Providing options to functions and issuing messages when things go wrong are common mechanisms for getting your programs to look and behave like built-in functions; they are introduced in Section 6.2.
The chapter concludes with several larger pieces of code that put many of these pieces together in one program.
- Type
- Chapter
- Information
- Essentials of Programming in Mathematica® , pp. 209 - 242Publisher: Cambridge University PressPrint publication year: 2015