Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Returning rules from functions inside a package context

  • To: mathgroup at smc.vnet.net
  • Subject: [mg82424] Re: Returning rules from functions inside a package context
  • From: Norbert Marxer <marxer at mec.li>
  • Date: Sat, 20 Oct 2007 05:45:37 -0400 (EDT)
  • References: <ff9rjl$4mv$1@smc.vnet.net>

On 19 Okt., 10:58, Art <grenan... at gmail.com> wrote:
> The following question has been asked and answered in a variety of
> ways in this forum, but I am new to Mathematica and am still not sure what is
> best practice after doing Trace of various trivial statements for the
> past few hours.
>
> For example, I would like f[] below to return {a->1.}, not
> {A`Private`a->1.}.
>
> BeginPackage["A`"]
> f::usage = ""
> Begin["`Private`"]
>
> f[] := FindFit[{{0, 0}, {1, 1}}, a x, {a}, x]
>
> End[]
>
> EndPackage[]
>
> The use of Rule[] in different scoping constructs is confusing.
>
> Thanks.

Hello

You could use Clear (to reset the global a;otherwise a on the left
hand side of your rule is replaced by its value) and ReplaceAll (to
replace the private a by the global a) in your function definition :
i.e.

BeginPackage["A`"];
f::usage = "";
Begin["`Private`"];
f[] := (Clear[Global`a]; FindFit[{{0, 0}, {1, 1}}, a*x, {a}, x] /.
{A`Private`a -> Global`a})
End[];
EndPackage[];

I'm sure there are other ways to get what you want.

Best Regards
Norbert Marxer






  • Prev by Date: Re: determine viewpoint value during V6 3d rotation
  • Next by Date: Re: Returning rules from functions inside a package context
  • Previous by thread: Returning rules from functions inside a package context
  • Next by thread: Re: Returning rules from functions inside a package context