Generating the Collatz sequence in F#

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





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite












Below is my code for building the collatz sequence so a given number. I'm trying to make the function tail recursive and I believe that this impelemtation is tail recursive but I'm not yet so comfortable with F# that I can convince myself 100%.



let collatzSeq n =
let collatz m =
match m%2 with
| 0 -> m/2
| _ -> 3*m + 1
let rec loop (l,acc) =
match l with
| 1 -> acc
| _ -> let l' = (collatz l) in loop (l',l'::acc)
loop (n,[n]) |> List.rev

[<EntryPoint>]
let main argv =
let col12 = collatzSeq 12
printfn "%A %d" col12 col12.Length
0 // return an integer exit code


My concern is does the let .. in loop construct stop this being tail recursive and if it does how do I improve the code to make it tail recursive?







share|improve this question

























    up vote
    4
    down vote

    favorite












    Below is my code for building the collatz sequence so a given number. I'm trying to make the function tail recursive and I believe that this impelemtation is tail recursive but I'm not yet so comfortable with F# that I can convince myself 100%.



    let collatzSeq n =
    let collatz m =
    match m%2 with
    | 0 -> m/2
    | _ -> 3*m + 1
    let rec loop (l,acc) =
    match l with
    | 1 -> acc
    | _ -> let l' = (collatz l) in loop (l',l'::acc)
    loop (n,[n]) |> List.rev

    [<EntryPoint>]
    let main argv =
    let col12 = collatzSeq 12
    printfn "%A %d" col12 col12.Length
    0 // return an integer exit code


    My concern is does the let .. in loop construct stop this being tail recursive and if it does how do I improve the code to make it tail recursive?







    share|improve this question





















      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      Below is my code for building the collatz sequence so a given number. I'm trying to make the function tail recursive and I believe that this impelemtation is tail recursive but I'm not yet so comfortable with F# that I can convince myself 100%.



      let collatzSeq n =
      let collatz m =
      match m%2 with
      | 0 -> m/2
      | _ -> 3*m + 1
      let rec loop (l,acc) =
      match l with
      | 1 -> acc
      | _ -> let l' = (collatz l) in loop (l',l'::acc)
      loop (n,[n]) |> List.rev

      [<EntryPoint>]
      let main argv =
      let col12 = collatzSeq 12
      printfn "%A %d" col12 col12.Length
      0 // return an integer exit code


      My concern is does the let .. in loop construct stop this being tail recursive and if it does how do I improve the code to make it tail recursive?







      share|improve this question











      Below is my code for building the collatz sequence so a given number. I'm trying to make the function tail recursive and I believe that this impelemtation is tail recursive but I'm not yet so comfortable with F# that I can convince myself 100%.



      let collatzSeq n =
      let collatz m =
      match m%2 with
      | 0 -> m/2
      | _ -> 3*m + 1
      let rec loop (l,acc) =
      match l with
      | 1 -> acc
      | _ -> let l' = (collatz l) in loop (l',l'::acc)
      loop (n,[n]) |> List.rev

      [<EntryPoint>]
      let main argv =
      let col12 = collatzSeq 12
      printfn "%A %d" col12 col12.Length
      0 // return an integer exit code


      My concern is does the let .. in loop construct stop this being tail recursive and if it does how do I improve the code to make it tail recursive?









      share|improve this question










      share|improve this question




      share|improve this question









      asked Jan 16 at 14:45









      Jackson

      238210




      238210




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          2
          down vote













          By building two implementaions of a much simpler function you can see that the function in the question is in a form that is tail recursive.



          let rec recList n =
          match n with
          | 1 -> [1]
          | _ -> n :: recList (n - 1)

          let accList n =
          let rec loop (l,acc) =
          match l with
          |1 -> acc
          |_ -> let l' = l - 1 in loop(l',l::acc)
          loop (n,) |> List.rev

          [<EntryPoint>]
          let main argv =

          // Fails with a stack overflow!
          // printfn "%A" (recList 100000)
          printfn "%A" (accList 10000000)


          The recList implementaion again demonstrates that a simple recursive function will cause the stack to explode, on my machine this happened between 10000 and 100000.



          The accList version which mirrors the function in the question works with values much larger than the one that caused the recList function to fail.






          share|improve this answer





















            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.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            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%2fcodereview.stackexchange.com%2fquestions%2f185223%2fgenerating-the-collatz-sequence-in-f%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            2
            down vote













            By building two implementaions of a much simpler function you can see that the function in the question is in a form that is tail recursive.



            let rec recList n =
            match n with
            | 1 -> [1]
            | _ -> n :: recList (n - 1)

            let accList n =
            let rec loop (l,acc) =
            match l with
            |1 -> acc
            |_ -> let l' = l - 1 in loop(l',l::acc)
            loop (n,) |> List.rev

            [<EntryPoint>]
            let main argv =

            // Fails with a stack overflow!
            // printfn "%A" (recList 100000)
            printfn "%A" (accList 10000000)


            The recList implementaion again demonstrates that a simple recursive function will cause the stack to explode, on my machine this happened between 10000 and 100000.



            The accList version which mirrors the function in the question works with values much larger than the one that caused the recList function to fail.






            share|improve this answer

























              up vote
              2
              down vote













              By building two implementaions of a much simpler function you can see that the function in the question is in a form that is tail recursive.



              let rec recList n =
              match n with
              | 1 -> [1]
              | _ -> n :: recList (n - 1)

              let accList n =
              let rec loop (l,acc) =
              match l with
              |1 -> acc
              |_ -> let l' = l - 1 in loop(l',l::acc)
              loop (n,) |> List.rev

              [<EntryPoint>]
              let main argv =

              // Fails with a stack overflow!
              // printfn "%A" (recList 100000)
              printfn "%A" (accList 10000000)


              The recList implementaion again demonstrates that a simple recursive function will cause the stack to explode, on my machine this happened between 10000 and 100000.



              The accList version which mirrors the function in the question works with values much larger than the one that caused the recList function to fail.






              share|improve this answer























                up vote
                2
                down vote










                up vote
                2
                down vote









                By building two implementaions of a much simpler function you can see that the function in the question is in a form that is tail recursive.



                let rec recList n =
                match n with
                | 1 -> [1]
                | _ -> n :: recList (n - 1)

                let accList n =
                let rec loop (l,acc) =
                match l with
                |1 -> acc
                |_ -> let l' = l - 1 in loop(l',l::acc)
                loop (n,) |> List.rev

                [<EntryPoint>]
                let main argv =

                // Fails with a stack overflow!
                // printfn "%A" (recList 100000)
                printfn "%A" (accList 10000000)


                The recList implementaion again demonstrates that a simple recursive function will cause the stack to explode, on my machine this happened between 10000 and 100000.



                The accList version which mirrors the function in the question works with values much larger than the one that caused the recList function to fail.






                share|improve this answer













                By building two implementaions of a much simpler function you can see that the function in the question is in a form that is tail recursive.



                let rec recList n =
                match n with
                | 1 -> [1]
                | _ -> n :: recList (n - 1)

                let accList n =
                let rec loop (l,acc) =
                match l with
                |1 -> acc
                |_ -> let l' = l - 1 in loop(l',l::acc)
                loop (n,) |> List.rev

                [<EntryPoint>]
                let main argv =

                // Fails with a stack overflow!
                // printfn "%A" (recList 100000)
                printfn "%A" (accList 10000000)


                The recList implementaion again demonstrates that a simple recursive function will cause the stack to explode, on my machine this happened between 10000 and 100000.



                The accList version which mirrors the function in the question works with values much larger than the one that caused the recList function to fail.







                share|improve this answer













                share|improve this answer



                share|improve this answer











                answered Jan 17 at 14:09









                Jackson

                238210




                238210






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f185223%2fgenerating-the-collatz-sequence-in-f%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Chat program with C++ and SFML

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    Will my employers contract hold up in court?