(*^ ::[ Information = "This is a Mathematica Notebook file. It contains ASCII text, and can be transferred by email, ftp, or other text-file transfer utility. It should be read or edited using a copy of Mathematica or MathReader. If you received this as email, use your mail application or copy/paste to save everything from the line containing (*^ down to the line containing ^*) into a plain text file. On some systems you may have to give the file a name ending with ".ma" to allow Mathematica to recognize it as a Notebook. The line below identifies what version of Mathematica created this file, but it can be opened using any other version as well."; FrontEndVersion = "NeXT Mathematica Notebook Front End Version 2.2"; NeXTStandardFontEncoding; fontset = title, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M0, N0, O457, bold, L-5, a0, e130, 24, "Helvetica"; ; fontset = subtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M0, N0, O457, L1, e6, 18, "Helvetica"; ; fontset = subsubtitle, inactive, noPageBreakBelow, noPageBreakInGroup, nohscroll, preserveAspect, groupLikeTitle, center, M0, N0, O457, italic, L1, e6, 12, "Times"; ; fontset = section, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, grayBox, M17, N0, O457, bold, L1, a24, e2, 14, "Helvetica"; ; fontset = subsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, blackBox, M14, N0, O457, bold, L1, a13, e2, 10, "Times"; ; fontset = subsubsection, inactive, noPageBreakBelow, nohscroll, preserveAspect, groupLikeSection, whiteBox, M14, N0, O457, L1, a12, 10, "Times"; ; fontset = text, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L0, a7, e2, 10, "Times"; ; fontset = smalltext, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L-3, a8, 8, "Helvetica"; ; fontset = input, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeInput, M0, N0, O457, bold, L-5, 12, "Courier"; ; fontset = output, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M0, N0, O457, L-5, 12, "Courier"; ; fontset = message, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M0, N0, O457, L1, e2, 10, "Times"; ; fontset = print, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M0, N0, O457, L-5, 12, "Courier"; ; fontset = info, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeOutput, M0, N0, O457, B65535, L-5, 12, "Courier"; ; fontset = postscript, PostScript, formatAsPostScript, output, inactive, noPageBreakInGroup, nowordwrap, preserveAspect, groupLikeGraphics, M0, N0, O457, l34, w282, h287, L1, a6, 12, "Courier"; ; fontset = name, inactive, noPageBreakInGroup, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, italic, B65535, L1, 10, "Times"; ; fontset = header, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 8, "Helvetica"; ; fontset = leftheader, inactive, M0, N0, O457, 8, "Helvetica"; ; fontset = footer, inactive, nohscroll, noKeepOnOnePage, preserveAspect, center, M0, N0, O457, L1, 9, "Helvetica"; ; fontset = leftfooter, inactive, M0, N0, O457, 9, "Helvetica"; ; fontset = help, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 10, "Times"; ; fontset = clipboard, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12; fontset = completions, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12, "Courier"; ; fontset = special1, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12; fontset = special2, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12; fontset = special3, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12; fontset = special4, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12; fontset = special5, inactive, nohscroll, noKeepOnOnePage, preserveAspect, M0, N0, O457, L1, 12; paletteColors = 128; showRuler; automaticGrouping; currentKernel; ] :[font = title; inactive; preserveAspect; startGroup] Package Design :[font = subtitle; inactive; preserveAspect] Todd Gayley Vertical Applications :[font = text; inactive; preserveAspect] Good design is the most important criterion for creating packages for distribution. It is easy to become immersed in the specifics of implementing the functions; however, it is the design that manifests itself to the user, not the implementation details. Even the cleverest, fastest code can be made useless if it is clumsily packaged. The time and thought that you put into design considerations will be well rewarded in increased usability, easier maintenance, and a shortened development cycle. :[font = text; inactive; preserveAspect] It is sometimes stated that the complete design specification for a programming project should be created before work on the actual code begins. This is an extreme view, especially given the ease of incremental, "build-as-you-go" development in the Mathematica environment. Even if you like to jump right into coding, make sure that you spend time considering the top-down aspects of your design. ;[s] 3:0,0;250,1;261,2;398,-1; 3:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] You are creating an interface to a suite of functionality, so keep in mind the primary goal of interface design: usability. The usability of your package will be enhanced by adhering to two simple principles: make it "look and feel" like Mathematica, and make it mesh well with the rest of the system. ;[s] 3:0,0;238,1;249,2;302,-1; 3:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] The built-in kernel functions and the standard packages have established many precedents for how Mathematica functions are expected to be named and to behave. Resist the temptation to come up with another style. No matter how sensible and logical your scheme seems to you, chances are it will look unfamiliar and feel uncomfortable to users. Don't envision that you are creating a complete stand-alone application; keep in mind that others will be using your package(s) in ways you cannot foresee. Design the functions so that they can be used as part of a "pipeline". Make sure that the input to your functions is easy to obtain from other Mathematica functions, and likewise that the output is in a form that is immediately useful for further processing. Another aspect of meshing with the system is ensuring that the state of the user's session will not be unduly modified by loading your package. ;[s] 5:0,0;97,1;108,2;641,3;652,4;901,-1; 5:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] The guidelines presented here will help ensure that your packages comply with these two principles. These are not unbendable rules, but they are well-tested guidelines that match users' expectations. Two important references on package design are Roman Maeder's Programming in Mathematica, Second Edition (Addison-Wesley 1991) and Stephen Wolfram's "Some Elements of Mathematica Design" (The Mathematica Journal, Volume 2, Number 2). ;[s] 7:0,0;263,1;291,2;368,3;379,4;389,5;412,6;434,-1; 7:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = section; inactive; Cclosed; preserveAspect; startGroup] Naming :[font = text; inactive; preserveAspect; endGroup] Use the conventions established by the built-in symbols for all your exported symbol names. Names should be full English words or sequences of words, with the first letter of each word capitalized. When in doubt, use the longer or more explicit term; avoid abbreviations and names that are not descriptive. If a function with similar functionality exists in Mathematica, use a similar name. Think carefully before using abbreviations or acronyms in function names, even when they are commonly used in your specialty. Keep the number of function names to a minimum, and instead use options and differing argument sequences to provide different behaviors. To ensure portability across all platforms, keep your package names unique within the first eight letters. ;[s] 3:0,0;358,1;490,2;556,-1; 3:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,2,10,0,0,0; :[font = section; inactive; Cclosed; preserveAspect; startGroup] Modularity :[font = text; inactive; preserveAspect] The structure of a package provides a means to control which symbols will be exported (i.e., visible to users after your package has loaded). Consult Maeder's book as the canonical reference on the structure of packages and the context issues that are involved. Here is the basic structure: ;[s] 3:0,0;87,1;91,2;290,-1; 3:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; preserveAspect] BeginPackage["MyPackage`"] Introduce all the exported symbols (including functions, variables, option names and their values, etc.) here by supplying usage messages. Any symbols that appear before Begin["`Private`"] will be visible to users, so make sure that only symbols you want exported appear. The method of supplying a usage message is not required--it is merely a convenient means of referring to the symbols, thus creating them in the package context, and at the same time ensuring that each one has a usage message attached. FirstFunction::usage = "FirstFunction does nothing." SecondFunction::usage = "SecondFunction does nothing." Ordering::usage = "Ordering is an option to FirstFunction. It specifies..." If your package will Protect symbols it defines at the end, make sure it unprotects them at the beginning. This will avoid a long series of "this symbol is protected" messages if users inadvertently read your package in twice during a session: Unprotect[FirstFunction, SecondFunction, Ordering] By defining a subcontext of the package context, you hide all the symbols you introduce in the implementation of your functions. By convention, use "`Private`" as the name of this subcontext: Begin["`Private`"] All the code for implementing your package goes here. End[] If you choose, you can Protect the symbols you introduced: Protect[FirstFunction, SecondFunction, Ordering] EndPackage[]; ;[s] 19:0,0;28,1;213,2;231,3;575,4;787,5;813,6;820,7;1051,8;1104,9;1267,10;1278,11;1311,12;1332,13;1390,14;1398,15;1425,16;1432,17;1461,18;1526,-1; 19:1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0;1,11,8,Times,0,12,0,0,0;1,10,8,Courier,1,12,0,0,0; :[font = text; inactive; preserveAspect] It is also good to include a set of special comments at the beginning of the file. In addition to supplying important information to the reader, they can be extracted by special utilities (e.g., the function Annotation in Utilities`Package`). A good example of these comments is the package Utilities`Package`. Most of the comment types are self-explanatory. The :PackageVersion: comment gives the revision number of the package itself (a number that is meaningful to the author), and the :Mathematica Version: comment gives the minimum version number of Mathematica required to run it. ;[s] 15:0,0;189,1;193,2;208,3;218,4;222,5;240,6;291,7;309,8;363,9;379,10;489,11;510,12;555,13;566,14;588,-1; 15:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; preserveAspect; endGroup] (* :Title: Package Utilities *) (* :Context: Utilities`Package` *) (* :Author: John M. Novak *) (* :Summary: This package provides various utilities for searching and working with Mathematica packages. *) (* :Package Version: 1.0 *) (* :Mathematica Version: 2.1 *) (* :Copyright: Copyright 1992-1993, Wolfram Research, Inc. *) (* :History: V. 1.0 June 92, by John M. Novak. *) (* :Keywords: packages, path, keywords *) (* :Limitations: *) (* :Discussion: *) :[font = section; inactive; Cclosed; preserveAspect; startGroup] Programming Style :[font = text; inactive; preserveAspect] A few quick comments on programming style are appropriate here. (Longer discussions of this issue can be found elsewhere.) Use transformation rules and functional programming where possible; procedural programming is less efficient, longer, and clumsier in Mathematica. You have achieved this goal to the extent that functions like Map, Apply, Replace, Table, Fold, and Function appear in your code, and ones like For, Do, While, and Append do not. ;[s] 23:0,0;257,1;268,2;332,3;335,4;337,5;342,6;344,7;351,8;353,9;358,10;360,11;364,12;370,13;378,14;414,15;417,16;419,17;421,18;423,19;428,20;434,21;440,22;449,-1; 23:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect; endGroup] Make sparing use of the Print function. You will notice that it is virtually never used by the built-in functions or the standard packages. If you have information to present to the user that is a warning or an error, use the Message function. Chances are that anything else truly meaningful should be returned from your function not printed out so it can be used in further processing. The liberal use of Print statements is a habit forged by use of more traditional languages like C and FORTRAN, which are not interactive and are locked into a small set of rigid data types. The same comments apply to the Input function. If you find yourself coding many Input statements to get input from the user and Print statements to display results, this is a good indication that you have not made the shift to a Mathematica mindset, and others will find using your package unfamiliar and inconvenient. In cases where you want to provide diagnostic output (for example, to alert users to the progress of a calculation), make printed output an option. I suggest the name Verbose->(True or False), with False being the default. ;[s] 21:0,0;24,1;29,2;226,3;233,4;406,5;411,6;608,7;613,8;657,9;662,10;705,11;710,12;806,13;817,14;1063,15;1077,16;1081,17;1087,18;1094,19;1099,20;1129,-1; 21:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = section; inactive; Cclosed; preserveAspect; startGroup] Argument Handling :[font = text; inactive; preserveAspect] If there are built-in functions that take similar arguments to your functions, use the same argument order. For example, in virtually all functions that take a filename or stream as an argument, it is the first argument. :[font = text; inactive; preserveAspect] Proper use of patterns on the left-hand side of function definitions can greatly simplify the code required on the right-hand side. For example, you could write a single rule with a generic pattern that requires you to do a lot of argument testing on the right-hand side: :[font = input; preserveAspect] f[args___] := Module[{var}, If[Head[{args}[[1]]] =!= List, do something ]; If[Length[{args}] == 1, do something, (* else *) do something else ]; etc... ] :[font = text; inactive; preserveAspect] Instead, define multiple rules that match specific cases: :[font = input; preserveAspect] f[x_List, y_] := something... f[x_] := something... etc... :[font = text; inactive; preserveAspect] You might also want to write a "fall-through" pattern that catches any cases not matched by the other definitions. This definition can trigger an appropriate error message and then return unevaluated. This technique is demonstrated in the "Error Handling" section that follows. :[font = text; inactive; preserveAspect] Learning to write nontrivial patterns is time well spent, as it will save you a lot of work validating arguments in the body of your functions; consult Section 2.3 of The Mathematica Book for more information. ;[s] 3:0,0;172,1;183,2;211,-1; 3:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] There are two ways of supplying nonrequired arguments to a function. The first, and most common, is to use Options. The second is to use the Optional pattern type. There are no hard and fast rules on when to use an Optional pattern, but it should be used sparingly, and only one Optional pattern should appear in any function definition (you will understand why if you try to write a definition with multiple Optional objects). Examples of built-in functions that use optional arguments are Log (where the optional argument is the base) and Map (where it is a level specification). The order in which the various types should appear in a definition is always required arguments first, followed by Optional ones, followed by Options. For example: ;[s] 19:0,0;107,1;114,2;141,3;149,4;215,5;223,6;279,7;287,8;409,9;417,10;491,11;494,12;541,13;544,14;697,15;705,16;724,17;731,18;745,-1; 19:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; preserveAspect] f[x_, level_Integer:1, opts___?OptionQ] := body... :[font = text; inactive; preserveAspect] Inside the body of the above function you can use the parameter level and be sure that it has a value--it will be 1 if the user left it out (as in f[a]). The opts___?OptionQ pattern is a standard idiom in Mathematica. It will match a (possibly empty) sequence of rules or a list of rules. The function OptionQ is not documented in The Mathematica Book, but it is an established part of the kernel since Version 2.0. It is a very clean and self-documenting way of matching options. ;[s] 13:0,0;64,1;69,2;147,3;151,4;158,5;173,6;205,7;216,8;302,9;309,10;335,11;346,12;482,-1; 13:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] There is also a standard way of extracting the values of options. This is discussed more completely in Maeder's book, but here is a brief example. Let's say the function f takes one option, Method, which has possible values Quick and Dirty. You might write a function like: ;[s] 9:0,0;171,1;172,2;191,3;197,4;225,5;230,6;235,7;240,8;275,-1; 9:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; preserveAspect] Options[f] = {Method->Quick} f[a_, opts___?OptionQ] := Module[{meth}, meth = Method /. {opts} /. Options[f]; etc... ] :[font = text; inactive; preserveAspect; endGroup] Using this technique, meth will be whatever value the user specified for the Method option, or the default value Quick if the option was not specified. ;[s] 7:0,0;22,1;26,2;77,3;83,4;113,5;118,6;152,-1; 7:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = section; inactive; Cclosed; preserveAspect; startGroup] Error Handling :[font = text; inactive; preserveAspect] User errors should be detected whenever possible, and meaningful messages should be generated. This is often overlooked in Mathematica programming, probably because it is not seen as a high priority and because it requires careful, and sometimes clever, programming for which there are few well-known idioms. ;[s] 3:0,0;123,1;134,2;314,-1; 3:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] When bad input is given to a function, or an error is detected during execution, you generally have three choices: try to recover from the error condition and continue with the calculation, bail out of the calculation and return the symbol $Failed, or bail out and return the input "unevaluated". If the error is not fatal, so that continuation is possible, it is generally a good idea to issue a Message indicating what has happened (for example, an invalid option value was given, and the default is being used instead). If the evaluation cannot be performed, it is simplest to just return $Failed. Any time you return $Failed, a message should also be issued. ;[s] 9:0,0;241,1;248,2;398,3;405,4;593,5;600,6;622,7;629,8;664,-1; 9:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] Most built-in functions return "unevaluated" when given inappropriate input. "Unevaluated" is in quotes because, of course, the functions have been evaluated--they are just unchanged by evaluation. Imitating this behavior in your own functions is a bit more complicated than returning $Failed. For many types of inappropriate input, your functions will behave this way without any effort on your part. For example, if you only define a rule for f when given a list as an argument and a user executes f[1] it will return unevaluated. Ideally, though, your function should issue an error message before returning. Here's how you could do that: ;[s] 7:0,0;285,1;292,2;445,3;446,4;500,5;504,6;641,-1; 7:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; initialization; preserveAspect] *) f::list1 = "The first argument to f must be a list."; f[a_List] := something f[_] := holycow /; Message[f::list1] (* :[font = input; initialization; preserveAspect; startGroup] *) f[1] (* :[font = message; inactive; preserveAspect] f::list1: The first argument to f must be a list. :[font = output; output; inactive; preserveAspect; endGroup] f[1] ;[o] f[1] :[font = text; inactive; preserveAspect] The last definition for f is a "catch-all" case that traps any calls to f where the first argument is not a list. The statement holycow is never executed because the Message function, which is the test in the Condition, doesn't return True. In other words, you write a phony rule for f that will never match any input (because the Condition always fails), but which triggers a side effect of displaying a Message. ;[s] 19:0,0;24,1;25,2;72,3;73,4;128,5;135,6;166,7;173,8;209,9;218,10;235,11;239,12;284,13;285,14;331,15;340,16;405,17;412,18;413,-1; 19:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] In the above example, we knew from pattern-matching that the input was invalid. If a more complex test of the input is required, you can put the Condition as the last statement in a Module: ;[s] 5:0,0;145,1;154,2;182,3;188,4;191,-1; 5:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; preserveAspect] f[x_List] := Module[{result, test}, some calculations here... test = true or false depending on calculations result /; test ] :[font = text; inactive; preserveAspect] If test is set to False, the Condition fails and Mathematica pretends that the match to f[x_List] never happened, moving on to try other rules for f. This is a little-known tidbit of Mathematica programming. Note that Condition must be the last statement in the Module (or Block or CompoundExpression) for it to work. ;[s] 21:0,0;18,1;23,2;29,3;38,4;49,5;60,6;88,7;97,8;147,9;148,10;183,11;194,12;218,13;227,14;262,15;268,16;273,17;278,18;282,19;300,20;319,-1; 21:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] A good practice is to use preexisting Mathematica messages. Many messages are used by more than one built-in function, and they are generally defined as though they belonged to the symbol General. When Mathematica looks for a message with name func::tag, it first looks to see if there is a definition for this specific message. If not, it looks for a message of the form General::tag, and if such a message exists it uses its text for the func::tag message. In this way many symbols can share a set of messages. In the example above I used f::list1 to indicate that a list is needed in argument 1. In fact, a message that expresses this is already available, General::list: ;[s] 17:0,0;38,1;49,2;188,3;195,4;202,5;213,6;244,7;253,8;372,9;384,10;440,11;449,12;541,13;549,14;660,15;673,16;676,-1; 17:1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; initialization; preserveAspect; startGroup] *) General::list (* :[font = output; output; inactive; preserveAspect; endGroup] "List expected at position `2` in `1`." ;[o] List expected at position `2` in `1`. :[font = input; initialization; preserveAspect; startGroup] *) ComposeList[a,b] (* :[font = message; inactive; preserveAspect] ComposeList::list: List expected at position 1 in ComposeList[a, b]. :[font = output; output; inactive; preserveAspect; endGroup] ComposeList[a, b] ;[o] ComposeList[a, b] :[font = text; inactive; preserveAspect] Note, though, that the placeholder `1` needs to be the entire f[...] expression. Here's how you can get at this value: ;[s] 3:0,0;62,1;68,2;119,-1; 3:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; initialization; preserveAspect] *) Clear[f] f[a_List] := something p:Literal[f[a_]] := holycow /; Message[f::list, HoldForm[p], 1] (* :[font = text; inactive; preserveAspect] Now f behaves just like a built-in function: ;[s] 3:0,0;4,1;5,2;45,-1; 3:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; initialization; preserveAspect; startGroup] *) f[1] (* :[font = message; inactive; preserveAspect] f::list: List expected at position 1 in f[1]. :[font = output; output; inactive; preserveAspect; endGroup; endGroup] f[1] ;[o] f[1] :[font = section; inactive; Cclosed; preserveAspect; startGroup] Tread Lightly on the System :[font = text; inactive; preserveAspect] The state of a user's session should not be unnecessarily changed by loading your package or evaluating one of its functions. For example, don't Unprotect and modify system symbols unless absolutely necessary, and never change their Attributes. Similarly, avoid changing global variables (i.e., System` globals, whose names usually begin with a $). Your package should never put symbols in the Global` context or rely on the presence of certain symbols in that context. ;[s] 13:0,0;145,1;154,2;233,3;243,4;289,5;293,6;295,7;302,8;345,9;346,10;394,11;401,12;469,-1; 13:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = text; inactive; preserveAspect] If you must Unprotect symbols other than your own, make sure that you restore them to their previous protection state. You can do this by using the idiom introduced by Maeder: ;[s] 3:0,0;12,1;21,2;177,-1; 3:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0; :[font = input; preserveAspect] protected = Unprotect[Symbol1, Symbol2] modify Symbol1 and Symbol2.... Protect[Evaluate[protected]] :[font = text; inactive; preserveAspect] If you turn off messages temporarily, make certain that you restore them to their original state, whether that was on or off. This is a particularly common error (you can see several examples in the standard packages). Here is a code fragment that shows how to properly turn messages off and back on: :[font = input; preserveAspect; endGroup] wasOn = Head[f::msg] =!= $Off; Off[f::msg]; code that would trigger the f::msg message.... If[wasOn, On[f::msg]]; :[font = section; inactive; Cclosed; preserveAspect; startGroup] Testing :[font = text; inactive; preserveAspect; endGroup; endGroup] Testing is important for any package, no matter how small. Developing a standard test suite for each package will not only help you find most of the bugs (you never find all of them), it will also help you maintain your code and serve as an invaluable final check on the design and implementation. Wolfram Research has a package called Testing.m (available on MathSource) that will help you create and manage a test suite. Ideally, the tests should exercise every branch of every function, including error conditions. Pay special attention to checking that your functions do something sensible with any type of input, no matter how improper. ;[s] 5:0,0;336,1;345,2;360,3;370,4;642,-1; 5:1,9,7,Times,0,10,0,0,0;1,7,6,Courier,1,10,0,0,0;1,9,7,Times,0,10,0,0,0;1,9,7,Times,2,10,0,0,0;1,9,7,Times,0,10,0,0,0; ^*)