Generating a pattern from user-defined rules

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I want to generate a list of $L$ and $S$ according to the following rule:
upon each iteration, replace



  • $S rightarrow L$

  • $L rightarrow LS$

Such that, starting with $L$:



$ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$



How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.










share|improve this question

























    up vote
    1
    down vote

    favorite












    I want to generate a list of $L$ and $S$ according to the following rule:
    upon each iteration, replace



    • $S rightarrow L$

    • $L rightarrow LS$

    Such that, starting with $L$:



    $ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$



    How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.










    share|improve this question























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I want to generate a list of $L$ and $S$ according to the following rule:
      upon each iteration, replace



      • $S rightarrow L$

      • $L rightarrow LS$

      Such that, starting with $L$:



      $ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$



      How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.










      share|improve this question













      I want to generate a list of $L$ and $S$ according to the following rule:
      upon each iteration, replace



      • $S rightarrow L$

      • $L rightarrow LS$

      Such that, starting with $L$:



      $ L rightarrow LS rightarrow LSL rightarrow LSLLS rightarrow LSLLSLSL rightarrow LSLLSLSLLSLLS rightarrow dots$



      How can I automate this with Mathematica? I just want to specify the starting letter and the number of iteration and I'd want it to spit out the final result.







      list-manipulation pattern-matching string-manipulation






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 57 mins ago









      SuperCiocia

      376210




      376210




















          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          For the 5th iterate:



          Nest[
          Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
          L,
          5
          ]



          L, S, L, L, S, L, S, L, L, S, L, L, S




          And for the list of first 5 iterates:



          NestList[
          Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
          L,
          5
          ]



          L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
          L, L, S, L, L, S, L, S, L, L, S, L, L, S




          Edit:



          SubstitutionSystem operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:



          L = "L";
          S = "S";
          a = StringJoin /@ NestList[
          Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
          L,
          20
          ]; // AbsoluteTiming // First
          b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First

          a == b



          0.004506



          0.001265



          True







          share|improve this answer






















          • What if I just wanted a random sequence of $L$ and $S$?
            – SuperCiocia
            36 mins ago










          • Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
            – Henrik Schumacher
            34 mins ago











          • Great, thank you.
            – SuperCiocia
            32 mins ago










          • You're welcome.
            – Henrik Schumacher
            29 mins ago










          • @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
            – Henrik Schumacher
            17 mins ago


















          up vote
          1
          down vote













          You may use ReplaceRepeatedwith its MaxIterationsoption.



          ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L], 
          MaxIterations -> 5]



          L, S, L, L, S, L, S, L, L, S, L, L, S



          Hope this helps.





          share




















            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "387"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181998%2fgenerating-a-pattern-from-user-defined-rules%23new-answer', 'question_page');

            );

            Post as a guest






























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote



            accepted










            For the 5th iterate:



            Nest[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, S, L, L, S, L, S, L, L, S, L, L, S




            And for the list of first 5 iterates:



            NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
            L, L, S, L, L, S, L, S, L, L, S, L, L, S




            Edit:



            SubstitutionSystem operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:



            L = "L";
            S = "S";
            a = StringJoin /@ NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            20
            ]; // AbsoluteTiming // First
            b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First

            a == b



            0.004506



            0.001265



            True







            share|improve this answer






















            • What if I just wanted a random sequence of $L$ and $S$?
              – SuperCiocia
              36 mins ago










            • Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
              – Henrik Schumacher
              34 mins ago











            • Great, thank you.
              – SuperCiocia
              32 mins ago










            • You're welcome.
              – Henrik Schumacher
              29 mins ago










            • @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
              – Henrik Schumacher
              17 mins ago















            up vote
            2
            down vote



            accepted










            For the 5th iterate:



            Nest[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, S, L, L, S, L, S, L, L, S, L, L, S




            And for the list of first 5 iterates:



            NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
            L, L, S, L, L, S, L, S, L, L, S, L, L, S




            Edit:



            SubstitutionSystem operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:



            L = "L";
            S = "S";
            a = StringJoin /@ NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            20
            ]; // AbsoluteTiming // First
            b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First

            a == b



            0.004506



            0.001265



            True







            share|improve this answer






















            • What if I just wanted a random sequence of $L$ and $S$?
              – SuperCiocia
              36 mins ago










            • Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
              – Henrik Schumacher
              34 mins ago











            • Great, thank you.
              – SuperCiocia
              32 mins ago










            • You're welcome.
              – Henrik Schumacher
              29 mins ago










            • @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
              – Henrik Schumacher
              17 mins ago













            up vote
            2
            down vote



            accepted







            up vote
            2
            down vote



            accepted






            For the 5th iterate:



            Nest[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, S, L, L, S, L, S, L, L, S, L, L, S




            And for the list of first 5 iterates:



            NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
            L, L, S, L, L, S, L, S, L, L, S, L, L, S




            Edit:



            SubstitutionSystem operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:



            L = "L";
            S = "S";
            a = StringJoin /@ NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            20
            ]; // AbsoluteTiming // First
            b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First

            a == b



            0.004506



            0.001265



            True







            share|improve this answer














            For the 5th iterate:



            Nest[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, S, L, L, S, L, S, L, L, S, L, L, S




            And for the list of first 5 iterates:



            NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            5
            ]



            L, L, S, L, S, L, L, S, L, L, S, L, S, L, L, S, L, S,
            L, L, S, L, L, S, L, S, L, L, S, L, L, S




            Edit:



            SubstitutionSystem operates on strings instead. It is a bit faster because it is specifically designed for Lindenmayer systems:



            L = "L";
            S = "S";
            a = StringJoin /@ NestList[
            Replace[#, L -> Sequence[L, S], S -> Sequence[L], 1] &,
            L,
            20
            ]; // AbsoluteTiming // First
            b = SubstitutionSystem["L" -> "LS", "S" -> "L", "L", 20]; // AbsoluteTiming // First

            a == b



            0.004506



            0.001265



            True








            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 18 mins ago

























            answered 42 mins ago









            Henrik Schumacher

            38.1k250109




            38.1k250109











            • What if I just wanted a random sequence of $L$ and $S$?
              – SuperCiocia
              36 mins ago










            • Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
              – Henrik Schumacher
              34 mins ago











            • Great, thank you.
              – SuperCiocia
              32 mins ago










            • You're welcome.
              – Henrik Schumacher
              29 mins ago










            • @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
              – Henrik Schumacher
              17 mins ago

















            • What if I just wanted a random sequence of $L$ and $S$?
              – SuperCiocia
              36 mins ago










            • Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
              – Henrik Schumacher
              34 mins ago











            • Great, thank you.
              – SuperCiocia
              32 mins ago










            • You're welcome.
              – Henrik Schumacher
              29 mins ago










            • @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
              – Henrik Schumacher
              17 mins ago
















            What if I just wanted a random sequence of $L$ and $S$?
            – SuperCiocia
            36 mins ago




            What if I just wanted a random sequence of $L$ and $S$?
            – SuperCiocia
            36 mins ago












            Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
            – Henrik Schumacher
            34 mins ago





            Then you can use RandomChoice: RandomChoice[L, S, 10000] will generate a list of 10000 random letter at once (uniformly distributed). You can also prescribe probabilities for each letter, e.g. RandomChoice[0.1, 0.9 -> L, S, 10000].
            – Henrik Schumacher
            34 mins ago













            Great, thank you.
            – SuperCiocia
            32 mins ago




            Great, thank you.
            – SuperCiocia
            32 mins ago












            You're welcome.
            – Henrik Schumacher
            29 mins ago




            You're welcome.
            – Henrik Schumacher
            29 mins ago












            @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
            – Henrik Schumacher
            17 mins ago





            @SuperCiocia Please forget what I said about SequenceReplace. Better use Replace with level specification 1. That does also not suffer from using symbols.
            – Henrik Schumacher
            17 mins ago











            up vote
            1
            down vote













            You may use ReplaceRepeatedwith its MaxIterationsoption.



            ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L], 
            MaxIterations -> 5]



            L, S, L, L, S, L, S, L, L, S, L, L, S



            Hope this helps.





            share
























              up vote
              1
              down vote













              You may use ReplaceRepeatedwith its MaxIterationsoption.



              ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L], 
              MaxIterations -> 5]



              L, S, L, L, S, L, S, L, L, S, L, L, S



              Hope this helps.





              share






















                up vote
                1
                down vote










                up vote
                1
                down vote









                You may use ReplaceRepeatedwith its MaxIterationsoption.



                ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L], 
                MaxIterations -> 5]



                L, S, L, L, S, L, S, L, L, S, L, L, S



                Hope this helps.





                share












                You may use ReplaceRepeatedwith its MaxIterationsoption.



                ReplaceRepeated[L, L -> Sequence[L, S], S -> Sequence[L], 
                MaxIterations -> 5]



                L, S, L, L, S, L, S, L, L, S, L, L, S



                Hope this helps.






                share











                share


                share










                answered 7 mins ago









                Edmund

                24.4k32995




                24.4k32995



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181998%2fgenerating-a-pattern-from-user-defined-rules%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke