|
|
Re: Quick Mathematica Question
Posted:
Jan 6, 2011 2:04 AM
|
|
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
|
|