Message Boards Message Boards

Work with functions defined inside a Package?

GROUPS:

I have functions defined inside a Begin Block of a Package in the normal manner (BeginPackage ... foo::usage="bar" ... Begin ... DegreesPerMeterAtmosphere[targelevdeg,startalt]: = targelevdeg+startalt ... etc.)

I tried to define variables that would be exposed by the packaged (as below) - but it does not work and I can't find the correct comments ion the manuals. This is the simplified code:

BeginPackage[ "FoundationFunctions`"]
    speedlight::usage = "The speed of light in meters/sec";
    DegreesPerMeterAtmosphere::usage = "DegreesPerMeterAtmosphere[targelevdeg,startalt] stuff"

Begin[ "Private`"]

speedlight:=299792458 (* m/s *);
DegreesPerMeterAtmosphere[targelevdeg_,startalt_]: = targelevdeg+startalt
End[]
EndPackage[]

But when I Get[] the package from a Notebook the function is pulled across but not the variable. Specifically Names["FoundationFunctions`*"] yields {"DegreesPerMeterAtmosphere"}.

Am I trying to do something that is silly (perhaps one cannot use packages to define/ encapsulate variables)? Or have I done the right thing in the wrong way?

POSTED BY: Andrew Macafee
Answer
4 months ago

Writing this let me see what was wrong. This works:

BeginPackage[ "Avc`" ]
    ss::usage=
       "ss zzzzzzzz"
    abc::usage=
       "abc yyyyyy"
    Begin[ "Private`" ]
    ss:=67
    abc[x_]:=x+20
  End[ ]
  EndPackage[ ]

The extra semi-colons seemed like a good idea (but they were not).

POSTED BY: Andrew Macafee
Answer
4 months ago

Use Needs - Not Get.

Needs is used for reading a package. Get is a function for evaluating an unstructured notebook of stuff that probably needs to be rewritten as a package.

But whatever you do, don't take away from this the idea that you shouldn't be using semicolons. The Wolfram Language in particular, has some very interesting behavior that can happen if you remove semicolons. Semicolons are good and healthy.

POSTED BY: Sean Clarke
Answer
4 months ago

Confused here because in this Wolfram example (Setting up Wolfram Language Packages) I see no semi-colons ...

BeginPackage["Collatz`"]

Collatz::usage =
        "Collatz[n] gives a list of the iterates in the 3n+1 problem,
        starting from n. The conjecture is that this sequence always
        terminates."

Begin["`Private`"]

Collatz[1] := {1}

Collatz[n_Integer]  := Prepend[Collatz[3 n + 1], n] /; OddQ[n] && n > 0

Collatz[n_Integer] := Prepend[Collatz[n/2], n] /; EvenQ[n] && n > 0

End[ ]

EndPackage[ ]

Does the example need to be corrected? Could you point me to a link that tells me how to use the semi-colons correctly?

Similarly the Wolfram documentation suggests Get as an option during package development (So I have a Notebook that (currently) Get's the package?

POSTED BY: Andrew Macafee
Answer
4 months ago

Group Abstract Group Abstract