MathGroup Archive 2006

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

Search the Archive

Re: Rules

  • To: mathgroup at smc.vnet.net
  • Subject: [mg66376] Re: Rules
  • From: Peter Pein <petsie at dordos.net>
  • Date: Thu, 11 May 2006 02:14:53 -0400 (EDT)
  • References: <e3shji$m3u$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Marcin Rak schrieb:
> Hey everyone,
> 
> I had two simple questions, and was wondering wether anyone could through
> something my way:
> 
> 1) When we have a list, call it AList, and we type the following
> AList /. {rule1}
> AList /. rule1
> what is the difference between the two?  And, how would I go about
> defining a rule for the variable AList, and a rule for the contents of the
> list - obviously these are two different concepts, yet how would you
> differentiate b/w the two?
> 
> Thanx to all
> MR
> 
Hi Marcin,

I try to answer your questions with an example:

Build a list:
alist = 32 + Prime /@ Range[10]
--> {34, 35, 37, 39, 43, 45, 49, 51, 55, 61}

Address the elements (Integers in this case):
alist /. (p_Integer)?PrimeQ :> p + 1
--> {34, 35, 38, 39, 44, 45, 49, 51, 55, 62}

Address the whole list:
alist /. l_List :> 1 + l
--> {35, 36, 38, 40, 44, 46, 50, 52, 56, 62}

Usage of brackets for more than one rule:
alist /. {_Integer?PrimeQ -> "prime", c_Integer :> Plus @@ Last /@ FactorInteger[c]}
--> {2, 2, "prime", 2, "prime", 3, 2, 2, 2, "prime"}


Have a look at the help ("Replace", "ReplaceAll", "Pattern") and follow the links in the documentation to explore the concept of pattern matching.

hth,
   Peter


  • Prev by Date: Re: NDSolve system of partial eqn
  • Next by Date: RE: Rules
  • Previous by thread: Re: Rules
  • Next by thread: RE: Rules