Re: dynamic programming inside a function
- To: mathgroup at smc.vnet.net
- Subject: [mg40422] Re: [mg40409] dynamic programming inside a function
- From: Dr Bob <majort at cox-internet.com>
- Date: Sat, 5 Apr 2003 03:57:17 -0500 (EST)
- References: <200304040623.BAA06118@smc.vnet.net>
- Reply-to: majort at cox-internet.com
- Sender: owner-wri-mathgroup at wolfram.com
This works (and some of the changes weren't necessary):
test[f_, {vars__}] := Block[{dummy, dummy2, vPat = Sequence @@ (Pattern[#,
Blank[]] & /@ {vars})},
Clear[temp];
dummy[temp[vPat], dummy2[temp[vars], f[vars]]]
/. {dummy -> SetDelayed, dummy2 -> Set}
]
test[Sin, {x}]
?? temp
temp[x_] := temp[x] = Sin[x]
test[g, {x, y}]
?? temp
temp[x_, y_] := temp[x, y] = g[x, y]
Bobby
On Fri, 4 Apr 2003 01:23:37 -0500 (EST), Ken Sale <kesale at llnl.gov> wrote:
> Hi MathGroup,
>
> I'm trying to figure out how to create a function that remembers its
> values inside a function definition (I want the temporary function to
> stay hidden in a package eventually. I've tried the following
>
> test[f_, vars_List] :=
> Module[{v, vPat},
> Clear[temp];
> vPat = Sequence @@ (Pattern[#, Blank[]] & /@ vars);
> v = Sequence @@ vars;
> temp[vPat] := temp[v] = f[v];
> ]
>
> When I evaluate
> test[Sin, {x}]
>
> I find that the definition of temp isn't what I want
>
> ?temp
>
> temp[x_] := temp[v$97] = Sin[v$97]
>
> What I'd like to get instead is
>
> temp[x_] := temp[x] = Sin[x]
>
> I've tried putting ReleaseHold around various parts of the assignment but
> so far haven't found a way to do what I want.
>
> Any help will be appreciated.
>
> Thanks,
> Ken
--
majort at cox-internet.com
Bobby R. Treat
- References:
- dynamic programming inside a function
- From: Ken Sale <kesale@llnl.gov>
- dynamic programming inside a function