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}]]

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]]
Head["H"] /. 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, 
"imax" -> 
6}, {"H" -> {0.198376, 0.385448, 0.438549, 0.818111, 0.314781, 
0.533971, 0.955322}, "imax" -> 7}}


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


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?


2 Answers 2


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.

  • $\begingroup$ 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? $\endgroup$
    – gogoolplex
    Commented Jun 26, 2012 at 10:57
  • $\begingroup$ @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. $\endgroup$
    – Ajasja
    Commented Jun 26, 2012 at 12:14
  • $\begingroup$ yes that is a good point, I am doing it now like this $\endgroup$
    – gogoolplex
    Commented Jun 26, 2012 at 12:17
  • $\begingroup$ @gogoolplex This might also interest you then. $\endgroup$
    – Ajasja
    Commented Jun 26, 2012 at 12:26
  • $\begingroup$ I agree. One definitely does not want one's analysis functions copuling. $\endgroup$ Commented Jun 26, 2012 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
  • $\begingroup$ thanks, I use now the method "H"->H, because this makes it easier to read $\endgroup$
    – gogoolplex
    Commented Jun 26, 2012 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.