MathGroup Archive 2011

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

Search the Archive

Local scoping of pattern names in rules--or not

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120642] Local scoping of pattern names in rules--or not
  • From: Kris Carlson <carlsonkw at gmail.com>
  • Date: Sun, 31 Jul 2011 23:37:32 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com

Hi,

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]

Out[562]=
{{a,7,b},{a,7,b},{a,7,b},{a,7,b},{a,7,b},{a,7,b},{a,7,b},{a,7,b},{a,7,b},{a,7,b}}

In[566]:= f3[aList]

Out[566]=
{{a,1,b},{a,2,b},{a,3,b},{a,4,b},{a,5,b},{a,6,b},{a,7,b},{a,8,b},{a,9,b},{a,10,b}}

Thanks,

Kris



  • 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