# Returning Replacement Rules in a function and using it later on

I would like a function which returns replacement rules as some internal mathematica functions are doing and use the results in other functions. I could not find any information on that and I'm wondering if it is a good idea after all. I'm not sure how this is done in a good way.

Here is an example:

Clear[f, f2, H, imax, res]
f[imax_] :=
Block[{H}, H = Table[Random[], {i, 1, imax}];
Return[{H -> H, imax -> imax}]]
f[5]


with the result

{{0.855859, 0.656278, 0.793888, 0.275233, 0.751709} -> {0.855859,
0.656278, 0.793888, 0.275233, 0.751709}, 5 -> 5}


Yes this is obvious but I would like to use the name H internally and also in the returned replacement rule. So I do this instead:

f2[imax_] :=
Block[{H}, H = Table[Random[], {i, 1, imax}];
Return[{"H" -> H, "imax" -> imax}]]
res = Table[f2[imax], {imax, 5, 7}]
(*now i want to invesgate the result with imax=6*)
p = First @Flatten[Position[res, "imax" -> 6]]
res[[p]]


with the result:

{{"H" -> {0.486493, 0.60306, 0.666644, 0.148913, 0.598069},
"imax" ->
5}, {"H" -> {0.873354, 0.98408, 0.0392209, 0.428918, 0.485521,
0.710918},
"imax" ->
6}, {"H" -> {0.198376, 0.385448, 0.438549, 0.818111, 0.314781,
0.533971, 0.955322}, "imax" -> 7}}

2

{"H" -> {0.873354, 0.98408, 0.0392209, 0.428918, 0.485521, 0.710918},
"imax" -> 6}

String


So the Problem is that Head["H"] /. res[[p]] results in a string and ListPlot["H"] /. res[[p]] would complain but plot the result. A good thing tough is that it is possible to search in the results and it is more flexible and readable than returning a List. So what is the best way to do this? So that I can use H and imax internally in the function and in the Return?

-

I think returning a list of rules is a great idea. I do it all the time to reduce the copuling between my various analysis functions.

You're almost there, you just have to get the replacement right.

instead of Head["H"] /. res[[p]] you have to use Head["H" /. res[[p]]] (notice I moved the first closing bracket to the end). The first form basically says "Replace String with a suitable rule" while the second says "Replace "H" with any suitable rule".

The same goes for ListPlot. ListPlot["H" /. res[[p]]] works without problems.

-
Ok, thanks, good to know that you use it do. BTW. how to you use the list of rules in your analysis functions? Do you use the whole list as one argument? –  gogoolplex Jun 26 '12 at 10:57
@gogoolplex I return the whole list as one argument and then pass it to the next functions. If in between my requirements change (for example if I have to calculate some errors or additional correlations), then I don't have to change my other functions, since the addition of one more replacement rule will not break anything. –  Ajasja Jun 26 '12 at 12:14
yes that is a good point, I am doing it now like this –  gogoolplex Jun 26 '12 at 12:17
@gogoolplex This might also interest you then. –  Ajasja Jun 26 '12 at 12:26
I agree. One definitely does not want one's analysis functions copuling. –  Daniel Lichtblau Jun 26 '12 at 16:49

It may be better if you don't use the same symbol internally and for the result rule. It surely complicates matters to do so.

As to using the rules, it boils down to this:

Head[x] /. x -> 1  (* Symbol  *)
Head[x /. x -> 1]  (* Integer *)


If you can use the replacement inside the function and the function does not have a Hold* attribute this is a solution. If however you need to apply the rule many times within an expression, or the function will break with the ReplaceAll inside it, you may consider one means or another of "injecting" the values.

Here is one method:

rules = {x -> 1, y -> 2};

Unevaluated[ {Print[y]; Head[x]} ] /. rules

-
thanks, I use now the method "H"->H, because this makes it easier to read –  gogoolplex Jun 26 '12 at 12:19