Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.
l = {"BreuschPagan" -> BreuschPagan[data, lm, coords], 
   "WhitesHeteroskedasticity" -> WhitesHeteroskedasticity[lm, coords]};

f = {"BreuschPagan", "WhitesHeteroskedasticity"}

In[360]:= l /. Rule[f_, _] :> f

Out[360]= {"BreuschPagan", "WhitesHeteroskedasticity"}`

Have tried reading the support but i just dont get it.

If I only apply l /. Rule[f_, _] the output becomes _

And why does l /. Rule[lof_, _] :> f give the out

{{"BreuschPagan", "WhitesHeteroskedasticity"}, {"BreuschPagan", 
share|improve this question

closed as off-topic by Yves Klett, ciao, bobthechemist, The Toad Mar 31 '14 at 2:03

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Yves Klett, ciao, bobthechemist, The Toad
If this question can be reworded to fit the rules in the help center, please edit the question.

Look at FullForm of Rule: FullForm[a -> 3] gives Rule[a,3] so x /. Rule[f_, _] :> f just says to extract the first argument of the rule (the thing before the arrow). So rules = {"a" -> 99, "b" -> 100}; rules /. Rule[f_, _] :> f returns {"a","b"} see reference.wolfram.com/mathematica/ref/Rule.html –  Nasser Mar 29 '14 at 23:58

1 Answer 1

up vote 3 down vote accepted

For all I know this question might get closed, but I'll attempt an answer anyway, for the cases not covered by Nasser in his comment.

The main source of confusion (I'm guessing) for you with the code fragments you posted is that Rule in some places is being used for doing pattern replacement (which is the usual case), and in other places it's the thing being matched against (perhaps less usual, but still perfectly valid.)

For clarity, I'll evaluate l in-place in the expression l /. Rule[f_, _] and rewrite Rule[f_, _] in its shorthand form, so we end up with:

{"BreuschPagan" -> BreuschPagan[data, lm, coords], 
   "WhitesHeteroskedasticity" -> WhitesHeteroskedasticity[lm, coords]} /. f_ -> _

In the above, the /. f_ -> _ bit says: match any expression (via the _) and replace it with _. The entire list get matched and replaced with _. The f here is just the name you're giving to the pattern, it has nothing to do with the function f that's been defined elsewhere. You could just as well have written l /. Rule[blah_, _] or l /. Rule[_, _] and it would give you the same answer.


l /. Rule[lof_, _] :> f

which I'll evaluate partially to get:

{"BreuschPagan" -> BreuschPagan[data, lm, coords], 
  "WhitesHeteroskedasticity" -> 
   WhitesHeteroskedasticity[lm, coords]} /. (lof_ -> _) :> f

You're saying, look for the pattern _ -> _ and replace it with f. Each of "BreuschPagan" -> BreuschPagan[data, lm, coords] and "WhitesHeteroskedasticity" -> WhitesHeteroskedasticity[lm, coords] match the pattern _ -> _, so basically (if f were undefined) you'd get {f, f} as the answer. However since f has been defined as f = {"BreuschPagan", "WhitesHeteroskedasticity"}, the expression {f, f} gets expanded to the final result that you got.

share|improve this answer
I should add that the RuleDelayed (i.e. :>) in the second example could be replaced by another Rule, i.e. you could write it as l /. Rule[lof_, _] -> f. It does matter in certain situations, but doesn't make a practical difference here. (I suppose it looks a bit nicer.) –  Aky Mar 30 '14 at 8:11

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