Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Quick Mathematica Question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115279] Re: Quick Mathematica Question
  • From: Charles Gillingham <cgillingham1 at me.com>
  • Date: Thu, 6 Jan 2011 02:04:56 -0500 (EST)

Try this:

------------------------

   (* Given a list of expressions return all possible applications of the \
   grammar rules to the expressions *)

  ApplyGrammar[ grammar_List, expressions_List ] := 
    Join @@ Map[ReplaceList[#, grammar] &, expressions]

  MyGrammar = {
     {a___, 0, b___} -> {a, 1, b},
     {a___, 1, b___} -> {a, 1, 0, b}
     };

------------------------
Note that the grammar rules have to be written a little strangely to get the pattern matcher to insert them in the sequence.

You can apply the function by hand, like so:

   {{0}, {1}} // TableForm
   ApplyGrammar[MyGrammar, %]  // TableForm
   ApplyGrammar[MyGrammar, %]  // TableForm
   ApplyGrammar[MyGrammar, %]  // TableForm
   ApplyGrammar[MyGrammar, %]  // TableForm
   ApplyGrammar[MyGrammar, %]  // TableForm

Or you can apply it using "Nest" or "Nestlist", like so:

   Nest[ApplyGrammar[MyGrammar, #] &, {{0}, {1}}, 4] // TableForm

or similarly with "NestList". 


On Jan 5, 2011, at 2:46 AM, Dean wrote:

> How would I program user-defined rules such as
> 
> 0->1
> 1->10
> 
> to give the output, and specify the number of recursions.  For example,
> starting with 0,
> 
> 0
> 1
> 10
> 101
> 10110
> ...
> 
> Specified, 5 generations.
> 
> -- 
> Dean Rosenthal
> 
> cell: 646 733 6966
> www.deanrosenthal.org
> http://www.the-open-space.org/webmag/test1.html


  • Prev by Date: Re: Quick Mathematica Question
  • Next by Date: Re: original meaning of System` functions
  • Previous by thread: Re: Quick Mathematica Question
  • Next by thread: Solving 2nd order PDE into Mathematica