MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Local scoping of pattern names in rules--or not

  • To: mathgroup at
  • Subject: [mg120642] Local scoping of pattern names in rules--or not
  • From: Kris Carlson <carlsonkw at>
  • Date: Sun, 31 Jul 2011 23:37:32 -0400 (EDT)
  • Delivered-to:


I am writing a function to find the shortest paths from a source vertex to a
target in a directed graph. The actual application of this is identifying
possible circuits in the spinal cord where an electrical stimulator
interferes with pain signals and alleviates the pain. To find direct
connections from source to target, even with 11K connections, is trivial,
but it's less so with intermediate nodes, variable path length in the # of
vertices, and recurrence. I could not find a built-in shortest paths
function with variable path length, or in Combinatorica. If you know of one
please let me know.

Sometimes the local scoping in Rule and Cases works and sometimes not. I'd
like to have a better understanding of this. Here is what Tech Support wrote
me a while ago after some initial confusion about the cause and solution
themselves, and a toy example:

Thank you for the email. I apologize for not providing explanations in my
notebook for what exactly Mathematica is doing. In fact I have figured out
that it is not a problem with your variables being symbols or lists but
rather that of evaluating the Rule itself. For some reason, even when the
variables are locally scoped, Mathematica is evaluating their values before
the rule is evaluated. Using a Rule Delayed (:>) solves this problem and
the results are as expected. I have inserted my comments/explanations in
 the attached notebook.

In[558]:= aList=Range[10];n=7;

In[563]:= f1[list1_List]:=Module[{},Cases[aList,n_/;n>3->50]]

f2[list1_List]:=Module[{},Cases[aList,n_->{a,n,b}]] (* local scoping doesn't
work as intended *)

In[565]:= f3[list1_List]:=Module[{},Cases[aList,n_:>{a,n,b}]]

In[564]:= f1[aList]

Out[564]= {50,50,50,50,50,50,50}

In[562]:= f2[aList]


In[566]:= f3[aList]




  • Prev by Date: Re: FinancialData errors
  • Next by Date: Re: In Version 8, Combinatorial&GraphTheory functions confuse me completely
  • Previous by thread: Another way to represent complex function graphically
  • Next by thread: Problems with matrices and clearing error messages