Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: Re: Quick Mathematica Question
Replies: 0  

Advanced Search

Back to Topic List Back to Topic List  
Charles Gillingham

Posts: 14
Registered: 10/30/10
Re: Quick Mathematica Question
Posted: Jan 6, 2011 2:04 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

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




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.