Not sure why this function returns a reversed array









up vote
1
down vote

favorite












I'm working on a JavaScript exercise and having some trouble untangling the logic as to why it works. It basically has a function called "mystery" that uses a bunch of very simple functions, and returns an array you give it but in reversed order. I've been sitting in front of a whiteboard for an hour trying to figure out the logic behind it, and not getting it. Can a kind soul take a look at these functions and explain how the mystery function returns a reversed array? Thank you!



function rest(arr) 
return arr.slice(1);



function first(arr)
return arr[0];


function conj(arr, value)
arr.push(value);
return arr;


function mystery(array)
if (array.length === 0)
return ;

return conj(mystery(rest(array)), first(array));










share|improve this question























  • Try to actually run it, use the debugger to trace through it, line by line...
    – obe
    Nov 10 at 21:14














up vote
1
down vote

favorite












I'm working on a JavaScript exercise and having some trouble untangling the logic as to why it works. It basically has a function called "mystery" that uses a bunch of very simple functions, and returns an array you give it but in reversed order. I've been sitting in front of a whiteboard for an hour trying to figure out the logic behind it, and not getting it. Can a kind soul take a look at these functions and explain how the mystery function returns a reversed array? Thank you!



function rest(arr) 
return arr.slice(1);



function first(arr)
return arr[0];


function conj(arr, value)
arr.push(value);
return arr;


function mystery(array)
if (array.length === 0)
return ;

return conj(mystery(rest(array)), first(array));










share|improve this question























  • Try to actually run it, use the debugger to trace through it, line by line...
    – obe
    Nov 10 at 21:14












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I'm working on a JavaScript exercise and having some trouble untangling the logic as to why it works. It basically has a function called "mystery" that uses a bunch of very simple functions, and returns an array you give it but in reversed order. I've been sitting in front of a whiteboard for an hour trying to figure out the logic behind it, and not getting it. Can a kind soul take a look at these functions and explain how the mystery function returns a reversed array? Thank you!



function rest(arr) 
return arr.slice(1);



function first(arr)
return arr[0];


function conj(arr, value)
arr.push(value);
return arr;


function mystery(array)
if (array.length === 0)
return ;

return conj(mystery(rest(array)), first(array));










share|improve this question















I'm working on a JavaScript exercise and having some trouble untangling the logic as to why it works. It basically has a function called "mystery" that uses a bunch of very simple functions, and returns an array you give it but in reversed order. I've been sitting in front of a whiteboard for an hour trying to figure out the logic behind it, and not getting it. Can a kind soul take a look at these functions and explain how the mystery function returns a reversed array? Thank you!



function rest(arr) 
return arr.slice(1);



function first(arr)
return arr[0];


function conj(arr, value)
arr.push(value);
return arr;


function mystery(array)
if (array.length === 0)
return ;

return conj(mystery(rest(array)), first(array));







javascript arrays






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 10 at 21:09

























asked Nov 10 at 21:06









amacdonald

529




529











  • Try to actually run it, use the debugger to trace through it, line by line...
    – obe
    Nov 10 at 21:14
















  • Try to actually run it, use the debugger to trace through it, line by line...
    – obe
    Nov 10 at 21:14















Try to actually run it, use the debugger to trace through it, line by line...
– obe
Nov 10 at 21:14




Try to actually run it, use the debugger to trace through it, line by line...
– obe
Nov 10 at 21:14












8 Answers
8






active

oldest

votes

















up vote
2
down vote













your first function "rest" removes the first element, as slice will return elements from 1 to the end of the array, then the "conj" function will take the first element that was removed (through the "first" function) and put it in the end, and doing so recursively it'll take elements from the beginning and put them to the end.






share|improve this answer



























    up vote
    2
    down vote













    mystery is a recursive function.



    It calls itself using the return value of the rest function, which returns everything except the first element.



    It uses the result of that + the result of first, which returns the first character, and concatenates them again (using conj), but with the first element at the end.



    So, say you put in [H e l l o],



    it will return conj(mystery([e l l o], H)



    mystery([e l l o]) will return conj(mystery([l l o], e)



    mystery([l l o]) will return conj(mystery([l o], l)



    and so on, until the array that goes into mistery is empty, in which case the recursion ends, and we bubble back up to the first call.



    Side note, recursion is often used for exercises like this, but although it has some specific uses, in many cases it's way more efficient to not use recursion, because the overhead of making another function call is relatively hard work, compared to other solutions using a simple loop to move or swap items around.



    You can see what is happening if you output some information:






    function rest(arr) 
    return arr.slice(1);



    function first(arr)
    return arr[0];


    function conj(arr, value)
    arr.push(value);
    return arr;


    function mystery(array, level)
    if (array.length === 0)
    console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
    return ;

    console.log('mystery level '+level+' is called with '+array+
    '. I will move '+first(array)+' to the end.');

    var result = conj(mystery(rest(array), level+1), first(array));
    console.log('returning '+result+' for level '+level);
    return result;


    console.log(mystery(['H','e','l','l','o'], 0));








    share|improve this answer





























      up vote
      1
      down vote













      To understand a function that uses recursion it can help to just assume for a moment that the recursive (nested) call returns what it should and then see how it builds on that to produce a correct result.



      Let's for example suppose that array is [1, 2, 3, 4]



      So this line:



       conj(mystery(rest(array)), first(array));


      ... has a recursive call of mystery. It gets as argument the array, but with the first element removed from it (that is what rest returns), so it gets [2, 3, 4]



      Now we will just assume that this recursive call of mystery does the right thing and reverses that array into [4, 3, 2]. Then in the above quoted code we see this result is concatenated with first(array) (which is the first value, i.e. 1). So we get [4, 3, 2, 1]. Correct!



      This teaches us that if we assume mystery does the job right for an array with n-1 values, it also does it right for n values.



      Now remains to see whether mystery deals correctly with the smallest case, i.e. when the array is empty. It is easy to see it returns the correct result in that case, i.e. an empty array.



      So putting those two things together you can see that mystery does the job correctly for all possible array sizes.






      share|improve this answer



























        up vote
        1
        down vote













        The .push method places the item to end of array.
        .slice(1) means “except the first item”



        Pseudocode



        1. Get array A (arg of mystery). If it is empty, return it

        2. Take rest (everything except the first). We will call the rest B

        3. Run this program on B (recursively)

        4. Append the first item of A to end of B


        conj = append value to arr



        first = get first item of arr



        rest = return everything except the first item



        mystery when array is empty = return empty array



        mystery when array is not empty = Take rest(array), run mystery on it, then append first of array






        share|improve this answer



























          up vote
          1
          down vote













          Yeah, the magic of recursion. To understand think about what it does if you call mystery with a 2-element array [1,2].



          rest(array) will then be [2] mystery(rest(array)) will also be [2]



          first(array) will be 1.



          Then you return conj([2], 1) which locically results in [2,1].



          Now the trick is the recursion. If you have 3 elements [0,1,2] and call mystery with it this will happen:



          • it will call mystery(rest(array)) with essentially is mystery([1,2]). That this returns [2,1] have we already seen.


          • first(array) will be 0

          • so it returns conj([2,1],0) which is logically [2,1,0].

          this now recusivly works for as many elements as you wish. Essentially mystery will be called for every element to place it after all elements.






          share|improve this answer



























            up vote
            1
            down vote













            One thing to note here is that mystery() is called recursively. I added some comments to illustrate what's going on on each step.






            function rest(arr) 
            console.log('take first n-1 elements of', arr);
            return arr.slice(1);



            function first(arr)
            return arr[0];


            function conj(arr, value)
            arr.push(value);
            console.log('combine', arr, 'with', value)
            return arr;


            function mystery(array)
            console.log('call mystery on ', array)
            if (array.length === 0)
            return ;

            return conj(mystery(rest(array)), first(array));


            mystery([0,1,2])








            share|improve this answer



























              up vote
              1
              down vote













              It's pretty simple. The reason for you not being able to see it is RECURSION. Couple things, that I would like you to notice:



              • mystery(array) is recursive function because it call itself until the array passed in is emplty

              • all of your work is happening here: return conj(mystery(rest(array)), first(array));

              I'm not going to talk more about recursive function here rather I will show how you can track each recursive call using console.log(). Check out my code below, I've added console.log() to make things more clear for you. Try running mystery with some array and see results. This will make sense to you.






              function rest(arr) 
              return arr.slice(1);



              function first(arr)
              console.log("Returning ",arr[0], "from first(arr).")
              return arr[0];


              function conj(arr, value)
              console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
              arr.push(value);
              console.log("Returning ",arr, " from Conj(arr,value)");
              return arr;


              function mystery(array)
              if (array.length === 0)
              console.log("array is emplty. So, returning empty array from mystery");
              return ;

              console.log("array is not emplty. So, calling mystery(array) again.");
              return conj(mystery(rest(array)), first(array));

              var reverse =mystery([1,2,3,4]);
              console.log("The final result", reverse);








              share|improve this answer



























                up vote
                0
                down vote













                Not gonna lie, this is a weird way to reverse the order of the array. It's basically using recursion to slice the first element of the array and conjoin the elements with first element at the end.



                So here's a walk though:



                mystery(['h','e','l','l','o']) ->



                first check if array is empty then method rest is called ->



                rest(['h','e','l','l','o']) ->



                rest slices the array at index 1 returning a new array of ['e','l','l','o'] ->



                then mystery is called which repeats the previous steps with the return value of rest ->



                mystery(['e','l','l','o']) ->



                this means the mystery will be keep getting called on until rest returns an empty array ->



                mystery(['e','l','l','o']) ->
                mystery(['l','l','o']) ->
                mystery(['l','o']) ->
                mystery(['o']) ->
                mystery() ->



                when mystery has an empty array it returns the empty array and then first is called ->

                first returns just the first element of the array and happens after mystery is returned it would look something like this before mystery is returned ->



                mystery(['e','l','l','o']) , first(['h','e','l','l','o']) ->

                so at the lowest level when mystery returns an empty array and first return the first element in ['o'] it would look like this ->



                , ['o'] ->
                conj would be called with these values ->
                conj( , ['o']) ->
                conj returns the combination of the two values ->
                conj( , ['o']) ->
                ['o'] ->

                this is then returned and repeated ->
                conj( , ['o']) ->
                conj(['o'] , ['l']) ->
                conj(['o','l'] , ['l']) ->
                conj(['o', 'l','l'] , ['e']) ->
                conj(['o', 'l','l', 'e'] , ['h']) ->
                ['o', 'l','l', 'e','h']






                share|improve this answer




















                  Your Answer






                  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: "1"
                  ;
                  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: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                   

                  draft saved


                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243401%2fnot-sure-why-this-function-returns-a-reversed-array%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  8 Answers
                  8






                  active

                  oldest

                  votes








                  8 Answers
                  8






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes








                  up vote
                  2
                  down vote













                  your first function "rest" removes the first element, as slice will return elements from 1 to the end of the array, then the "conj" function will take the first element that was removed (through the "first" function) and put it in the end, and doing so recursively it'll take elements from the beginning and put them to the end.






                  share|improve this answer
























                    up vote
                    2
                    down vote













                    your first function "rest" removes the first element, as slice will return elements from 1 to the end of the array, then the "conj" function will take the first element that was removed (through the "first" function) and put it in the end, and doing so recursively it'll take elements from the beginning and put them to the end.






                    share|improve this answer






















                      up vote
                      2
                      down vote










                      up vote
                      2
                      down vote









                      your first function "rest" removes the first element, as slice will return elements from 1 to the end of the array, then the "conj" function will take the first element that was removed (through the "first" function) and put it in the end, and doing so recursively it'll take elements from the beginning and put them to the end.






                      share|improve this answer












                      your first function "rest" removes the first element, as slice will return elements from 1 to the end of the array, then the "conj" function will take the first element that was removed (through the "first" function) and put it in the end, and doing so recursively it'll take elements from the beginning and put them to the end.







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 10 at 21:16









                      Reda Drissi

                      169111




                      169111






















                          up vote
                          2
                          down vote













                          mystery is a recursive function.



                          It calls itself using the return value of the rest function, which returns everything except the first element.



                          It uses the result of that + the result of first, which returns the first character, and concatenates them again (using conj), but with the first element at the end.



                          So, say you put in [H e l l o],



                          it will return conj(mystery([e l l o], H)



                          mystery([e l l o]) will return conj(mystery([l l o], e)



                          mystery([l l o]) will return conj(mystery([l o], l)



                          and so on, until the array that goes into mistery is empty, in which case the recursion ends, and we bubble back up to the first call.



                          Side note, recursion is often used for exercises like this, but although it has some specific uses, in many cases it's way more efficient to not use recursion, because the overhead of making another function call is relatively hard work, compared to other solutions using a simple loop to move or swap items around.



                          You can see what is happening if you output some information:






                          function rest(arr) 
                          return arr.slice(1);



                          function first(arr)
                          return arr[0];


                          function conj(arr, value)
                          arr.push(value);
                          return arr;


                          function mystery(array, level)
                          if (array.length === 0)
                          console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
                          return ;

                          console.log('mystery level '+level+' is called with '+array+
                          '. I will move '+first(array)+' to the end.');

                          var result = conj(mystery(rest(array), level+1), first(array));
                          console.log('returning '+result+' for level '+level);
                          return result;


                          console.log(mystery(['H','e','l','l','o'], 0));








                          share|improve this answer


























                            up vote
                            2
                            down vote













                            mystery is a recursive function.



                            It calls itself using the return value of the rest function, which returns everything except the first element.



                            It uses the result of that + the result of first, which returns the first character, and concatenates them again (using conj), but with the first element at the end.



                            So, say you put in [H e l l o],



                            it will return conj(mystery([e l l o], H)



                            mystery([e l l o]) will return conj(mystery([l l o], e)



                            mystery([l l o]) will return conj(mystery([l o], l)



                            and so on, until the array that goes into mistery is empty, in which case the recursion ends, and we bubble back up to the first call.



                            Side note, recursion is often used for exercises like this, but although it has some specific uses, in many cases it's way more efficient to not use recursion, because the overhead of making another function call is relatively hard work, compared to other solutions using a simple loop to move or swap items around.



                            You can see what is happening if you output some information:






                            function rest(arr) 
                            return arr.slice(1);



                            function first(arr)
                            return arr[0];


                            function conj(arr, value)
                            arr.push(value);
                            return arr;


                            function mystery(array, level)
                            if (array.length === 0)
                            console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
                            return ;

                            console.log('mystery level '+level+' is called with '+array+
                            '. I will move '+first(array)+' to the end.');

                            var result = conj(mystery(rest(array), level+1), first(array));
                            console.log('returning '+result+' for level '+level);
                            return result;


                            console.log(mystery(['H','e','l','l','o'], 0));








                            share|improve this answer
























                              up vote
                              2
                              down vote










                              up vote
                              2
                              down vote









                              mystery is a recursive function.



                              It calls itself using the return value of the rest function, which returns everything except the first element.



                              It uses the result of that + the result of first, which returns the first character, and concatenates them again (using conj), but with the first element at the end.



                              So, say you put in [H e l l o],



                              it will return conj(mystery([e l l o], H)



                              mystery([e l l o]) will return conj(mystery([l l o], e)



                              mystery([l l o]) will return conj(mystery([l o], l)



                              and so on, until the array that goes into mistery is empty, in which case the recursion ends, and we bubble back up to the first call.



                              Side note, recursion is often used for exercises like this, but although it has some specific uses, in many cases it's way more efficient to not use recursion, because the overhead of making another function call is relatively hard work, compared to other solutions using a simple loop to move or swap items around.



                              You can see what is happening if you output some information:






                              function rest(arr) 
                              return arr.slice(1);



                              function first(arr)
                              return arr[0];


                              function conj(arr, value)
                              arr.push(value);
                              return arr;


                              function mystery(array, level)
                              if (array.length === 0)
                              console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
                              return ;

                              console.log('mystery level '+level+' is called with '+array+
                              '. I will move '+first(array)+' to the end.');

                              var result = conj(mystery(rest(array), level+1), first(array));
                              console.log('returning '+result+' for level '+level);
                              return result;


                              console.log(mystery(['H','e','l','l','o'], 0));








                              share|improve this answer














                              mystery is a recursive function.



                              It calls itself using the return value of the rest function, which returns everything except the first element.



                              It uses the result of that + the result of first, which returns the first character, and concatenates them again (using conj), but with the first element at the end.



                              So, say you put in [H e l l o],



                              it will return conj(mystery([e l l o], H)



                              mystery([e l l o]) will return conj(mystery([l l o], e)



                              mystery([l l o]) will return conj(mystery([l o], l)



                              and so on, until the array that goes into mistery is empty, in which case the recursion ends, and we bubble back up to the first call.



                              Side note, recursion is often used for exercises like this, but although it has some specific uses, in many cases it's way more efficient to not use recursion, because the overhead of making another function call is relatively hard work, compared to other solutions using a simple loop to move or swap items around.



                              You can see what is happening if you output some information:






                              function rest(arr) 
                              return arr.slice(1);



                              function first(arr)
                              return arr[0];


                              function conj(arr, value)
                              arr.push(value);
                              return arr;


                              function mystery(array, level)
                              if (array.length === 0)
                              console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
                              return ;

                              console.log('mystery level '+level+' is called with '+array+
                              '. I will move '+first(array)+' to the end.');

                              var result = conj(mystery(rest(array), level+1), first(array));
                              console.log('returning '+result+' for level '+level);
                              return result;


                              console.log(mystery(['H','e','l','l','o'], 0));








                              function rest(arr) 
                              return arr.slice(1);



                              function first(arr)
                              return arr[0];


                              function conj(arr, value)
                              arr.push(value);
                              return arr;


                              function mystery(array, level)
                              if (array.length === 0)
                              console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
                              return ;

                              console.log('mystery level '+level+' is called with '+array+
                              '. I will move '+first(array)+' to the end.');

                              var result = conj(mystery(rest(array), level+1), first(array));
                              console.log('returning '+result+' for level '+level);
                              return result;


                              console.log(mystery(['H','e','l','l','o'], 0));





                              function rest(arr) 
                              return arr.slice(1);



                              function first(arr)
                              return arr[0];


                              function conj(arr, value)
                              arr.push(value);
                              return arr;


                              function mystery(array, level)
                              if (array.length === 0)
                              console.log('mystery level '+level+' is called with an empty array. Recursion ends here.. Stay tuned for the answer.');
                              return ;

                              console.log('mystery level '+level+' is called with '+array+
                              '. I will move '+first(array)+' to the end.');

                              var result = conj(mystery(rest(array), level+1), first(array));
                              console.log('returning '+result+' for level '+level);
                              return result;


                              console.log(mystery(['H','e','l','l','o'], 0));






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Nov 11 at 0:50

























                              answered Nov 10 at 21:18









                              GolezTrol

                              96.9k9129171




                              96.9k9129171




















                                  up vote
                                  1
                                  down vote













                                  To understand a function that uses recursion it can help to just assume for a moment that the recursive (nested) call returns what it should and then see how it builds on that to produce a correct result.



                                  Let's for example suppose that array is [1, 2, 3, 4]



                                  So this line:



                                   conj(mystery(rest(array)), first(array));


                                  ... has a recursive call of mystery. It gets as argument the array, but with the first element removed from it (that is what rest returns), so it gets [2, 3, 4]



                                  Now we will just assume that this recursive call of mystery does the right thing and reverses that array into [4, 3, 2]. Then in the above quoted code we see this result is concatenated with first(array) (which is the first value, i.e. 1). So we get [4, 3, 2, 1]. Correct!



                                  This teaches us that if we assume mystery does the job right for an array with n-1 values, it also does it right for n values.



                                  Now remains to see whether mystery deals correctly with the smallest case, i.e. when the array is empty. It is easy to see it returns the correct result in that case, i.e. an empty array.



                                  So putting those two things together you can see that mystery does the job correctly for all possible array sizes.






                                  share|improve this answer
























                                    up vote
                                    1
                                    down vote













                                    To understand a function that uses recursion it can help to just assume for a moment that the recursive (nested) call returns what it should and then see how it builds on that to produce a correct result.



                                    Let's for example suppose that array is [1, 2, 3, 4]



                                    So this line:



                                     conj(mystery(rest(array)), first(array));


                                    ... has a recursive call of mystery. It gets as argument the array, but with the first element removed from it (that is what rest returns), so it gets [2, 3, 4]



                                    Now we will just assume that this recursive call of mystery does the right thing and reverses that array into [4, 3, 2]. Then in the above quoted code we see this result is concatenated with first(array) (which is the first value, i.e. 1). So we get [4, 3, 2, 1]. Correct!



                                    This teaches us that if we assume mystery does the job right for an array with n-1 values, it also does it right for n values.



                                    Now remains to see whether mystery deals correctly with the smallest case, i.e. when the array is empty. It is easy to see it returns the correct result in that case, i.e. an empty array.



                                    So putting those two things together you can see that mystery does the job correctly for all possible array sizes.






                                    share|improve this answer






















                                      up vote
                                      1
                                      down vote










                                      up vote
                                      1
                                      down vote









                                      To understand a function that uses recursion it can help to just assume for a moment that the recursive (nested) call returns what it should and then see how it builds on that to produce a correct result.



                                      Let's for example suppose that array is [1, 2, 3, 4]



                                      So this line:



                                       conj(mystery(rest(array)), first(array));


                                      ... has a recursive call of mystery. It gets as argument the array, but with the first element removed from it (that is what rest returns), so it gets [2, 3, 4]



                                      Now we will just assume that this recursive call of mystery does the right thing and reverses that array into [4, 3, 2]. Then in the above quoted code we see this result is concatenated with first(array) (which is the first value, i.e. 1). So we get [4, 3, 2, 1]. Correct!



                                      This teaches us that if we assume mystery does the job right for an array with n-1 values, it also does it right for n values.



                                      Now remains to see whether mystery deals correctly with the smallest case, i.e. when the array is empty. It is easy to see it returns the correct result in that case, i.e. an empty array.



                                      So putting those two things together you can see that mystery does the job correctly for all possible array sizes.






                                      share|improve this answer












                                      To understand a function that uses recursion it can help to just assume for a moment that the recursive (nested) call returns what it should and then see how it builds on that to produce a correct result.



                                      Let's for example suppose that array is [1, 2, 3, 4]



                                      So this line:



                                       conj(mystery(rest(array)), first(array));


                                      ... has a recursive call of mystery. It gets as argument the array, but with the first element removed from it (that is what rest returns), so it gets [2, 3, 4]



                                      Now we will just assume that this recursive call of mystery does the right thing and reverses that array into [4, 3, 2]. Then in the above quoted code we see this result is concatenated with first(array) (which is the first value, i.e. 1). So we get [4, 3, 2, 1]. Correct!



                                      This teaches us that if we assume mystery does the job right for an array with n-1 values, it also does it right for n values.



                                      Now remains to see whether mystery deals correctly with the smallest case, i.e. when the array is empty. It is easy to see it returns the correct result in that case, i.e. an empty array.



                                      So putting those two things together you can see that mystery does the job correctly for all possible array sizes.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 10 at 21:15









                                      trincot

                                      113k1477109




                                      113k1477109




















                                          up vote
                                          1
                                          down vote













                                          The .push method places the item to end of array.
                                          .slice(1) means “except the first item”



                                          Pseudocode



                                          1. Get array A (arg of mystery). If it is empty, return it

                                          2. Take rest (everything except the first). We will call the rest B

                                          3. Run this program on B (recursively)

                                          4. Append the first item of A to end of B


                                          conj = append value to arr



                                          first = get first item of arr



                                          rest = return everything except the first item



                                          mystery when array is empty = return empty array



                                          mystery when array is not empty = Take rest(array), run mystery on it, then append first of array






                                          share|improve this answer
























                                            up vote
                                            1
                                            down vote













                                            The .push method places the item to end of array.
                                            .slice(1) means “except the first item”



                                            Pseudocode



                                            1. Get array A (arg of mystery). If it is empty, return it

                                            2. Take rest (everything except the first). We will call the rest B

                                            3. Run this program on B (recursively)

                                            4. Append the first item of A to end of B


                                            conj = append value to arr



                                            first = get first item of arr



                                            rest = return everything except the first item



                                            mystery when array is empty = return empty array



                                            mystery when array is not empty = Take rest(array), run mystery on it, then append first of array






                                            share|improve this answer






















                                              up vote
                                              1
                                              down vote










                                              up vote
                                              1
                                              down vote









                                              The .push method places the item to end of array.
                                              .slice(1) means “except the first item”



                                              Pseudocode



                                              1. Get array A (arg of mystery). If it is empty, return it

                                              2. Take rest (everything except the first). We will call the rest B

                                              3. Run this program on B (recursively)

                                              4. Append the first item of A to end of B


                                              conj = append value to arr



                                              first = get first item of arr



                                              rest = return everything except the first item



                                              mystery when array is empty = return empty array



                                              mystery when array is not empty = Take rest(array), run mystery on it, then append first of array






                                              share|improve this answer












                                              The .push method places the item to end of array.
                                              .slice(1) means “except the first item”



                                              Pseudocode



                                              1. Get array A (arg of mystery). If it is empty, return it

                                              2. Take rest (everything except the first). We will call the rest B

                                              3. Run this program on B (recursively)

                                              4. Append the first item of A to end of B


                                              conj = append value to arr



                                              first = get first item of arr



                                              rest = return everything except the first item



                                              mystery when array is empty = return empty array



                                              mystery when array is not empty = Take rest(array), run mystery on it, then append first of array







                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 10 at 21:16









                                              jiwopene

                                              1688




                                              1688




















                                                  up vote
                                                  1
                                                  down vote













                                                  Yeah, the magic of recursion. To understand think about what it does if you call mystery with a 2-element array [1,2].



                                                  rest(array) will then be [2] mystery(rest(array)) will also be [2]



                                                  first(array) will be 1.



                                                  Then you return conj([2], 1) which locically results in [2,1].



                                                  Now the trick is the recursion. If you have 3 elements [0,1,2] and call mystery with it this will happen:



                                                  • it will call mystery(rest(array)) with essentially is mystery([1,2]). That this returns [2,1] have we already seen.


                                                  • first(array) will be 0

                                                  • so it returns conj([2,1],0) which is logically [2,1,0].

                                                  this now recusivly works for as many elements as you wish. Essentially mystery will be called for every element to place it after all elements.






                                                  share|improve this answer
























                                                    up vote
                                                    1
                                                    down vote













                                                    Yeah, the magic of recursion. To understand think about what it does if you call mystery with a 2-element array [1,2].



                                                    rest(array) will then be [2] mystery(rest(array)) will also be [2]



                                                    first(array) will be 1.



                                                    Then you return conj([2], 1) which locically results in [2,1].



                                                    Now the trick is the recursion. If you have 3 elements [0,1,2] and call mystery with it this will happen:



                                                    • it will call mystery(rest(array)) with essentially is mystery([1,2]). That this returns [2,1] have we already seen.


                                                    • first(array) will be 0

                                                    • so it returns conj([2,1],0) which is logically [2,1,0].

                                                    this now recusivly works for as many elements as you wish. Essentially mystery will be called for every element to place it after all elements.






                                                    share|improve this answer






















                                                      up vote
                                                      1
                                                      down vote










                                                      up vote
                                                      1
                                                      down vote









                                                      Yeah, the magic of recursion. To understand think about what it does if you call mystery with a 2-element array [1,2].



                                                      rest(array) will then be [2] mystery(rest(array)) will also be [2]



                                                      first(array) will be 1.



                                                      Then you return conj([2], 1) which locically results in [2,1].



                                                      Now the trick is the recursion. If you have 3 elements [0,1,2] and call mystery with it this will happen:



                                                      • it will call mystery(rest(array)) with essentially is mystery([1,2]). That this returns [2,1] have we already seen.


                                                      • first(array) will be 0

                                                      • so it returns conj([2,1],0) which is logically [2,1,0].

                                                      this now recusivly works for as many elements as you wish. Essentially mystery will be called for every element to place it after all elements.






                                                      share|improve this answer












                                                      Yeah, the magic of recursion. To understand think about what it does if you call mystery with a 2-element array [1,2].



                                                      rest(array) will then be [2] mystery(rest(array)) will also be [2]



                                                      first(array) will be 1.



                                                      Then you return conj([2], 1) which locically results in [2,1].



                                                      Now the trick is the recursion. If you have 3 elements [0,1,2] and call mystery with it this will happen:



                                                      • it will call mystery(rest(array)) with essentially is mystery([1,2]). That this returns [2,1] have we already seen.


                                                      • first(array) will be 0

                                                      • so it returns conj([2,1],0) which is logically [2,1,0].

                                                      this now recusivly works for as many elements as you wish. Essentially mystery will be called for every element to place it after all elements.







                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Nov 10 at 21:16









                                                      Lux

                                                      10.5k32658




                                                      10.5k32658




















                                                          up vote
                                                          1
                                                          down vote













                                                          One thing to note here is that mystery() is called recursively. I added some comments to illustrate what's going on on each step.






                                                          function rest(arr) 
                                                          console.log('take first n-1 elements of', arr);
                                                          return arr.slice(1);



                                                          function first(arr)
                                                          return arr[0];


                                                          function conj(arr, value)
                                                          arr.push(value);
                                                          console.log('combine', arr, 'with', value)
                                                          return arr;


                                                          function mystery(array)
                                                          console.log('call mystery on ', array)
                                                          if (array.length === 0)
                                                          return ;

                                                          return conj(mystery(rest(array)), first(array));


                                                          mystery([0,1,2])








                                                          share|improve this answer
























                                                            up vote
                                                            1
                                                            down vote













                                                            One thing to note here is that mystery() is called recursively. I added some comments to illustrate what's going on on each step.






                                                            function rest(arr) 
                                                            console.log('take first n-1 elements of', arr);
                                                            return arr.slice(1);



                                                            function first(arr)
                                                            return arr[0];


                                                            function conj(arr, value)
                                                            arr.push(value);
                                                            console.log('combine', arr, 'with', value)
                                                            return arr;


                                                            function mystery(array)
                                                            console.log('call mystery on ', array)
                                                            if (array.length === 0)
                                                            return ;

                                                            return conj(mystery(rest(array)), first(array));


                                                            mystery([0,1,2])








                                                            share|improve this answer






















                                                              up vote
                                                              1
                                                              down vote










                                                              up vote
                                                              1
                                                              down vote









                                                              One thing to note here is that mystery() is called recursively. I added some comments to illustrate what's going on on each step.






                                                              function rest(arr) 
                                                              console.log('take first n-1 elements of', arr);
                                                              return arr.slice(1);



                                                              function first(arr)
                                                              return arr[0];


                                                              function conj(arr, value)
                                                              arr.push(value);
                                                              console.log('combine', arr, 'with', value)
                                                              return arr;


                                                              function mystery(array)
                                                              console.log('call mystery on ', array)
                                                              if (array.length === 0)
                                                              return ;

                                                              return conj(mystery(rest(array)), first(array));


                                                              mystery([0,1,2])








                                                              share|improve this answer












                                                              One thing to note here is that mystery() is called recursively. I added some comments to illustrate what's going on on each step.






                                                              function rest(arr) 
                                                              console.log('take first n-1 elements of', arr);
                                                              return arr.slice(1);



                                                              function first(arr)
                                                              return arr[0];


                                                              function conj(arr, value)
                                                              arr.push(value);
                                                              console.log('combine', arr, 'with', value)
                                                              return arr;


                                                              function mystery(array)
                                                              console.log('call mystery on ', array)
                                                              if (array.length === 0)
                                                              return ;

                                                              return conj(mystery(rest(array)), first(array));


                                                              mystery([0,1,2])








                                                              function rest(arr) 
                                                              console.log('take first n-1 elements of', arr);
                                                              return arr.slice(1);



                                                              function first(arr)
                                                              return arr[0];


                                                              function conj(arr, value)
                                                              arr.push(value);
                                                              console.log('combine', arr, 'with', value)
                                                              return arr;


                                                              function mystery(array)
                                                              console.log('call mystery on ', array)
                                                              if (array.length === 0)
                                                              return ;

                                                              return conj(mystery(rest(array)), first(array));


                                                              mystery([0,1,2])





                                                              function rest(arr) 
                                                              console.log('take first n-1 elements of', arr);
                                                              return arr.slice(1);



                                                              function first(arr)
                                                              return arr[0];


                                                              function conj(arr, value)
                                                              arr.push(value);
                                                              console.log('combine', arr, 'with', value)
                                                              return arr;


                                                              function mystery(array)
                                                              console.log('call mystery on ', array)
                                                              if (array.length === 0)
                                                              return ;

                                                              return conj(mystery(rest(array)), first(array));


                                                              mystery([0,1,2])






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Nov 10 at 21:22









                                                              shkaper

                                                              603311




                                                              603311




















                                                                  up vote
                                                                  1
                                                                  down vote













                                                                  It's pretty simple. The reason for you not being able to see it is RECURSION. Couple things, that I would like you to notice:



                                                                  • mystery(array) is recursive function because it call itself until the array passed in is emplty

                                                                  • all of your work is happening here: return conj(mystery(rest(array)), first(array));

                                                                  I'm not going to talk more about recursive function here rather I will show how you can track each recursive call using console.log(). Check out my code below, I've added console.log() to make things more clear for you. Try running mystery with some array and see results. This will make sense to you.






                                                                  function rest(arr) 
                                                                  return arr.slice(1);



                                                                  function first(arr)
                                                                  console.log("Returning ",arr[0], "from first(arr).")
                                                                  return arr[0];


                                                                  function conj(arr, value)
                                                                  console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
                                                                  arr.push(value);
                                                                  console.log("Returning ",arr, " from Conj(arr,value)");
                                                                  return arr;


                                                                  function mystery(array)
                                                                  if (array.length === 0)
                                                                  console.log("array is emplty. So, returning empty array from mystery");
                                                                  return ;

                                                                  console.log("array is not emplty. So, calling mystery(array) again.");
                                                                  return conj(mystery(rest(array)), first(array));

                                                                  var reverse =mystery([1,2,3,4]);
                                                                  console.log("The final result", reverse);








                                                                  share|improve this answer
























                                                                    up vote
                                                                    1
                                                                    down vote













                                                                    It's pretty simple. The reason for you not being able to see it is RECURSION. Couple things, that I would like you to notice:



                                                                    • mystery(array) is recursive function because it call itself until the array passed in is emplty

                                                                    • all of your work is happening here: return conj(mystery(rest(array)), first(array));

                                                                    I'm not going to talk more about recursive function here rather I will show how you can track each recursive call using console.log(). Check out my code below, I've added console.log() to make things more clear for you. Try running mystery with some array and see results. This will make sense to you.






                                                                    function rest(arr) 
                                                                    return arr.slice(1);



                                                                    function first(arr)
                                                                    console.log("Returning ",arr[0], "from first(arr).")
                                                                    return arr[0];


                                                                    function conj(arr, value)
                                                                    console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
                                                                    arr.push(value);
                                                                    console.log("Returning ",arr, " from Conj(arr,value)");
                                                                    return arr;


                                                                    function mystery(array)
                                                                    if (array.length === 0)
                                                                    console.log("array is emplty. So, returning empty array from mystery");
                                                                    return ;

                                                                    console.log("array is not emplty. So, calling mystery(array) again.");
                                                                    return conj(mystery(rest(array)), first(array));

                                                                    var reverse =mystery([1,2,3,4]);
                                                                    console.log("The final result", reverse);








                                                                    share|improve this answer






















                                                                      up vote
                                                                      1
                                                                      down vote










                                                                      up vote
                                                                      1
                                                                      down vote









                                                                      It's pretty simple. The reason for you not being able to see it is RECURSION. Couple things, that I would like you to notice:



                                                                      • mystery(array) is recursive function because it call itself until the array passed in is emplty

                                                                      • all of your work is happening here: return conj(mystery(rest(array)), first(array));

                                                                      I'm not going to talk more about recursive function here rather I will show how you can track each recursive call using console.log(). Check out my code below, I've added console.log() to make things more clear for you. Try running mystery with some array and see results. This will make sense to you.






                                                                      function rest(arr) 
                                                                      return arr.slice(1);



                                                                      function first(arr)
                                                                      console.log("Returning ",arr[0], "from first(arr).")
                                                                      return arr[0];


                                                                      function conj(arr, value)
                                                                      console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
                                                                      arr.push(value);
                                                                      console.log("Returning ",arr, " from Conj(arr,value)");
                                                                      return arr;


                                                                      function mystery(array)
                                                                      if (array.length === 0)
                                                                      console.log("array is emplty. So, returning empty array from mystery");
                                                                      return ;

                                                                      console.log("array is not emplty. So, calling mystery(array) again.");
                                                                      return conj(mystery(rest(array)), first(array));

                                                                      var reverse =mystery([1,2,3,4]);
                                                                      console.log("The final result", reverse);








                                                                      share|improve this answer












                                                                      It's pretty simple. The reason for you not being able to see it is RECURSION. Couple things, that I would like you to notice:



                                                                      • mystery(array) is recursive function because it call itself until the array passed in is emplty

                                                                      • all of your work is happening here: return conj(mystery(rest(array)), first(array));

                                                                      I'm not going to talk more about recursive function here rather I will show how you can track each recursive call using console.log(). Check out my code below, I've added console.log() to make things more clear for you. Try running mystery with some array and see results. This will make sense to you.






                                                                      function rest(arr) 
                                                                      return arr.slice(1);



                                                                      function first(arr)
                                                                      console.log("Returning ",arr[0], "from first(arr).")
                                                                      return arr[0];


                                                                      function conj(arr, value)
                                                                      console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
                                                                      arr.push(value);
                                                                      console.log("Returning ",arr, " from Conj(arr,value)");
                                                                      return arr;


                                                                      function mystery(array)
                                                                      if (array.length === 0)
                                                                      console.log("array is emplty. So, returning empty array from mystery");
                                                                      return ;

                                                                      console.log("array is not emplty. So, calling mystery(array) again.");
                                                                      return conj(mystery(rest(array)), first(array));

                                                                      var reverse =mystery([1,2,3,4]);
                                                                      console.log("The final result", reverse);








                                                                      function rest(arr) 
                                                                      return arr.slice(1);



                                                                      function first(arr)
                                                                      console.log("Returning ",arr[0], "from first(arr).")
                                                                      return arr[0];


                                                                      function conj(arr, value)
                                                                      console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
                                                                      arr.push(value);
                                                                      console.log("Returning ",arr, " from Conj(arr,value)");
                                                                      return arr;


                                                                      function mystery(array)
                                                                      if (array.length === 0)
                                                                      console.log("array is emplty. So, returning empty array from mystery");
                                                                      return ;

                                                                      console.log("array is not emplty. So, calling mystery(array) again.");
                                                                      return conj(mystery(rest(array)), first(array));

                                                                      var reverse =mystery([1,2,3,4]);
                                                                      console.log("The final result", reverse);





                                                                      function rest(arr) 
                                                                      return arr.slice(1);



                                                                      function first(arr)
                                                                      console.log("Returning ",arr[0], "from first(arr).")
                                                                      return arr[0];


                                                                      function conj(arr, value)
                                                                      console.log("Pushing ",value, " to ",arr, " in conj(arr,value)");
                                                                      arr.push(value);
                                                                      console.log("Returning ",arr, " from Conj(arr,value)");
                                                                      return arr;


                                                                      function mystery(array)
                                                                      if (array.length === 0)
                                                                      console.log("array is emplty. So, returning empty array from mystery");
                                                                      return ;

                                                                      console.log("array is not emplty. So, calling mystery(array) again.");
                                                                      return conj(mystery(rest(array)), first(array));

                                                                      var reverse =mystery([1,2,3,4]);
                                                                      console.log("The final result", reverse);






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Nov 10 at 22:11









                                                                      b1k

                                                                      737




                                                                      737




















                                                                          up vote
                                                                          0
                                                                          down vote













                                                                          Not gonna lie, this is a weird way to reverse the order of the array. It's basically using recursion to slice the first element of the array and conjoin the elements with first element at the end.



                                                                          So here's a walk though:



                                                                          mystery(['h','e','l','l','o']) ->



                                                                          first check if array is empty then method rest is called ->



                                                                          rest(['h','e','l','l','o']) ->



                                                                          rest slices the array at index 1 returning a new array of ['e','l','l','o'] ->



                                                                          then mystery is called which repeats the previous steps with the return value of rest ->



                                                                          mystery(['e','l','l','o']) ->



                                                                          this means the mystery will be keep getting called on until rest returns an empty array ->



                                                                          mystery(['e','l','l','o']) ->
                                                                          mystery(['l','l','o']) ->
                                                                          mystery(['l','o']) ->
                                                                          mystery(['o']) ->
                                                                          mystery() ->



                                                                          when mystery has an empty array it returns the empty array and then first is called ->

                                                                          first returns just the first element of the array and happens after mystery is returned it would look something like this before mystery is returned ->



                                                                          mystery(['e','l','l','o']) , first(['h','e','l','l','o']) ->

                                                                          so at the lowest level when mystery returns an empty array and first return the first element in ['o'] it would look like this ->



                                                                          , ['o'] ->
                                                                          conj would be called with these values ->
                                                                          conj( , ['o']) ->
                                                                          conj returns the combination of the two values ->
                                                                          conj( , ['o']) ->
                                                                          ['o'] ->

                                                                          this is then returned and repeated ->
                                                                          conj( , ['o']) ->
                                                                          conj(['o'] , ['l']) ->
                                                                          conj(['o','l'] , ['l']) ->
                                                                          conj(['o', 'l','l'] , ['e']) ->
                                                                          conj(['o', 'l','l', 'e'] , ['h']) ->
                                                                          ['o', 'l','l', 'e','h']






                                                                          share|improve this answer
























                                                                            up vote
                                                                            0
                                                                            down vote













                                                                            Not gonna lie, this is a weird way to reverse the order of the array. It's basically using recursion to slice the first element of the array and conjoin the elements with first element at the end.



                                                                            So here's a walk though:



                                                                            mystery(['h','e','l','l','o']) ->



                                                                            first check if array is empty then method rest is called ->



                                                                            rest(['h','e','l','l','o']) ->



                                                                            rest slices the array at index 1 returning a new array of ['e','l','l','o'] ->



                                                                            then mystery is called which repeats the previous steps with the return value of rest ->



                                                                            mystery(['e','l','l','o']) ->



                                                                            this means the mystery will be keep getting called on until rest returns an empty array ->



                                                                            mystery(['e','l','l','o']) ->
                                                                            mystery(['l','l','o']) ->
                                                                            mystery(['l','o']) ->
                                                                            mystery(['o']) ->
                                                                            mystery() ->



                                                                            when mystery has an empty array it returns the empty array and then first is called ->

                                                                            first returns just the first element of the array and happens after mystery is returned it would look something like this before mystery is returned ->



                                                                            mystery(['e','l','l','o']) , first(['h','e','l','l','o']) ->

                                                                            so at the lowest level when mystery returns an empty array and first return the first element in ['o'] it would look like this ->



                                                                            , ['o'] ->
                                                                            conj would be called with these values ->
                                                                            conj( , ['o']) ->
                                                                            conj returns the combination of the two values ->
                                                                            conj( , ['o']) ->
                                                                            ['o'] ->

                                                                            this is then returned and repeated ->
                                                                            conj( , ['o']) ->
                                                                            conj(['o'] , ['l']) ->
                                                                            conj(['o','l'] , ['l']) ->
                                                                            conj(['o', 'l','l'] , ['e']) ->
                                                                            conj(['o', 'l','l', 'e'] , ['h']) ->
                                                                            ['o', 'l','l', 'e','h']






                                                                            share|improve this answer






















                                                                              up vote
                                                                              0
                                                                              down vote










                                                                              up vote
                                                                              0
                                                                              down vote









                                                                              Not gonna lie, this is a weird way to reverse the order of the array. It's basically using recursion to slice the first element of the array and conjoin the elements with first element at the end.



                                                                              So here's a walk though:



                                                                              mystery(['h','e','l','l','o']) ->



                                                                              first check if array is empty then method rest is called ->



                                                                              rest(['h','e','l','l','o']) ->



                                                                              rest slices the array at index 1 returning a new array of ['e','l','l','o'] ->



                                                                              then mystery is called which repeats the previous steps with the return value of rest ->



                                                                              mystery(['e','l','l','o']) ->



                                                                              this means the mystery will be keep getting called on until rest returns an empty array ->



                                                                              mystery(['e','l','l','o']) ->
                                                                              mystery(['l','l','o']) ->
                                                                              mystery(['l','o']) ->
                                                                              mystery(['o']) ->
                                                                              mystery() ->



                                                                              when mystery has an empty array it returns the empty array and then first is called ->

                                                                              first returns just the first element of the array and happens after mystery is returned it would look something like this before mystery is returned ->



                                                                              mystery(['e','l','l','o']) , first(['h','e','l','l','o']) ->

                                                                              so at the lowest level when mystery returns an empty array and first return the first element in ['o'] it would look like this ->



                                                                              , ['o'] ->
                                                                              conj would be called with these values ->
                                                                              conj( , ['o']) ->
                                                                              conj returns the combination of the two values ->
                                                                              conj( , ['o']) ->
                                                                              ['o'] ->

                                                                              this is then returned and repeated ->
                                                                              conj( , ['o']) ->
                                                                              conj(['o'] , ['l']) ->
                                                                              conj(['o','l'] , ['l']) ->
                                                                              conj(['o', 'l','l'] , ['e']) ->
                                                                              conj(['o', 'l','l', 'e'] , ['h']) ->
                                                                              ['o', 'l','l', 'e','h']






                                                                              share|improve this answer












                                                                              Not gonna lie, this is a weird way to reverse the order of the array. It's basically using recursion to slice the first element of the array and conjoin the elements with first element at the end.



                                                                              So here's a walk though:



                                                                              mystery(['h','e','l','l','o']) ->



                                                                              first check if array is empty then method rest is called ->



                                                                              rest(['h','e','l','l','o']) ->



                                                                              rest slices the array at index 1 returning a new array of ['e','l','l','o'] ->



                                                                              then mystery is called which repeats the previous steps with the return value of rest ->



                                                                              mystery(['e','l','l','o']) ->



                                                                              this means the mystery will be keep getting called on until rest returns an empty array ->



                                                                              mystery(['e','l','l','o']) ->
                                                                              mystery(['l','l','o']) ->
                                                                              mystery(['l','o']) ->
                                                                              mystery(['o']) ->
                                                                              mystery() ->



                                                                              when mystery has an empty array it returns the empty array and then first is called ->

                                                                              first returns just the first element of the array and happens after mystery is returned it would look something like this before mystery is returned ->



                                                                              mystery(['e','l','l','o']) , first(['h','e','l','l','o']) ->

                                                                              so at the lowest level when mystery returns an empty array and first return the first element in ['o'] it would look like this ->



                                                                              , ['o'] ->
                                                                              conj would be called with these values ->
                                                                              conj( , ['o']) ->
                                                                              conj returns the combination of the two values ->
                                                                              conj( , ['o']) ->
                                                                              ['o'] ->

                                                                              this is then returned and repeated ->
                                                                              conj( , ['o']) ->
                                                                              conj(['o'] , ['l']) ->
                                                                              conj(['o','l'] , ['l']) ->
                                                                              conj(['o', 'l','l'] , ['e']) ->
                                                                              conj(['o', 'l','l', 'e'] , ['h']) ->
                                                                              ['o', 'l','l', 'e','h']







                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Nov 10 at 21:26









                                                                              Thatalent

                                                                              3268




                                                                              3268



























                                                                                   

                                                                                  draft saved


                                                                                  draft discarded















































                                                                                   


                                                                                  draft saved


                                                                                  draft discarded














                                                                                  StackExchange.ready(
                                                                                  function ()
                                                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243401%2fnot-sure-why-this-function-returns-a-reversed-array%23new-answer', 'question_page');

                                                                                  );

                                                                                  Post as a guest















                                                                                  Required, but never shown





















































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown

































                                                                                  Required, but never shown














                                                                                  Required, but never shown












                                                                                  Required, but never shown







                                                                                  Required, but never shown







                                                                                  這個網誌中的熱門文章

                                                                                  Barbados

                                                                                  How to read a connectionString WITH PROVIDER in .NET Core?

                                                                                  Node.js Script on GitHub Pages or Amazon S3