Re: Making a function dynamically define another conditional function...
- To: mathgroup at smc.vnet.net
- Subject: [mg21774] Re: Making a function dynamically define another conditional function...
- From: Tom Burton <tburton at brahea.com>
- Date: Thu, 27 Jan 2000 22:56:55 -0500 (EST)
- Organization: Brahea, Inc.
- References: <86md60$2cr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On 26 Jan 2000 04:01:52 -0500, in comp.soft-sys.math.mathematica you
wrote asking how to make the following code work:
>In[1] := TestFn[data_List] := Module[{args},
> ClearAll[f];
> Do[
> args = data[[i]];
> f[x_] = x + args[[1]] /; x==args[[2]],
> {i, Length[data]}
> ]]
I assume that you accidently omitted the colon from "f[x_] := ...". Then
two ways occur to me off the top: the easy way,
TestFn[data_List] :=
(ClearAll[f]; Apply[
Function[{a, A},
f[x_] := x + a /;
x == A], data, 1])
and the hard way,
TestFn[data_List] := Module[{args, a, A}, ClearAll[f];
Do[args = data[[i]];
ReleaseHold[
Hold[f[x_] := x + a /; x == A] /. {a -> args[[1]],
A -> args[[2]]}], {i, Length[data]}]]
The easy way finesses the problem by avoiding the need for an evaluation
within the body of the delayed assignment. The hard way illustrates how
I would force such an evaluation in three steps.
(1) Hold the assignment.
(2) Perform the evaluation with ReplaceAll (/.).
(3) Release the hold.
Tom Burton