Introduction to Programming 
with 
Mathematica

By
Richard J. Gaylord 
(gaylord@ux1.cso.uiuc.edu)

Samuel N. Kamin 
(kamin@cs.uiuc.edu)

Paul R. Wellin 
(wellin@sonoma.edu)

Copyright © 1993 by TELOS/Springer-Verlag

Chapter 11 – Contexts and Packages

Functions defined in this Notebook...(page no. in book)

BaseConvert...273
DefaultBase...273
StringToInt...273
numeralToInt...273

Initialization (evaluate to turn off spell messages)

Off[General::spell]

Off[General::spell1]

Section 11.1  Using Packages

Needs["Calculus`VectorAnalysis`"]

CrossProduct[{1.0, 2.0, 3.0}, {-1.0, -2.0, -3.0}]

Section 11.4  The BaseConvert Package

BeginPackage["BaseConvert`"]

DefaultBase::usage = "Default base used by StringToInt."

StringToInt::usage = "StringToInt[str, b] converts str to an
	integer, assuming it is written in base b. StringToInt[str] 
	is equivalent to StringToInt[str, DefaultBase]."

Begin["`Private`"]

DefaultBase = 10;
	StringToInt[str_] := StringToInt[str, DefaultBase]
	
	StringToInt[str_String, b_Integer] :=
		Which[
			Not[2 <= b <= 10], Print["StringToInt: second argument is
									  inappropriate base."],
			Not[Apply[And,Map[(0<=numeralToInt[#]<b)&,
								ToCharacterCode[str]]]],
			Print["StringToInt: first argument has
				incorrect digits."],
			True, (* arguments are legal *)
				Map[numeralToInt, ToCharacterCode[str]] .
		Table[b^i, {i,StringLength[str]-1,0,-1}]]

StringToInt[str_, b_] :=
		(If[Not[StringQ[str]],
			Print["StringToInt: ", str, " is not a string."]];
			If[Not[IntegerQ[b]],
				Print["StringToInt: ", b," is not an integer."]])
					StringToInt[x___] :=
				Print["StringToInt: called with ", Length[{x}],
						" arguments; 1 or 2 arguments are 
						 expected."]

numeralToInt[numeral_] := numeral - ToCharacterCode["0"][[1]]

End[]

EndPackage[]