I am following the excellent book by Roman Maeder, Programming in Mathematica, and now I am trying to create my first package.
I want to write down a package that lets the end user to use the following function f1
, whereas in turn f1
makes use of the function n
:
f1[x_] := Range[n[x]]
n[k_] := 10 + k
That is, for f1[3]
, for example, you get the list
{1,2,3,4,…,13}
(because you add 10 to 3).
I would like to have the community's opinion about this Minimal Working package.
Let's put aside things related to error messages, pattern matching and/or conditions in the input of the arguments for f1
and n
. For example, one doesn't really need to define the function n
, simply use a pure function, but I rather want to emphasize the concept of making use of two functions in order to complete a computation.
Maeder notes the following in the beginning of §1.2 that
- Auxiliary functions or private variables that are used inside the package would be accessible to the user. User could rely on the details of the implementation or make changes to it.
- One can pass variables as arguments that are also used locally inside the function.
Am I missing any of these in this package?
In particular, should I use a Module
in the definition of f1
?
Full Code:
(* ::Package:: *)
(* :Title: My first package *)
(* :Author: Ehud Behar *)
BeginPackage["MyPackage`"]
Begin["`Private`"]
f1[x_] :=
Range[1, n[x]]
n[k_] :=
With[{add = 10}, (*decide by how much to lengthen the list*)
add+k
]
End[]
EndPackage[]