Saturday 15 March 2014

recursion - Recursively append the first element of the list to the rest of a list -


I'm quite new to the plan and I want to take a list (1 2 3 4) and it's a function that returns Come (4 1 2 3) on a second run return (3 4 1 2) and make a correct move list with each call on the function.

The way I thought for the first time to solve this problem, it is already the last value of the re-listing, so in the plan I will add the CD of the list to the CDR of the list and The CDR will return back to its function unless only one final swap is not available.

Although I am not great at making recursive work and I have trouble doing this in a new language as such a plan. This is what I have tried to give an idea about where I want to head.

  (define (list) (if empty list) ('(adver (cshar lst)))))  

What you can do best to see which of your list jobs are available, Take a look at the documentation of your interpreter and use them to create a solution. For example, using a racket, a direct solution will be completed:

  (define (rshift lst) (opposition, stick together solution) (last list); select last item (LEST LE (All 1 (length lst)))));   

Give it a try:

  (rShift '(1 2 3 4)) => '(4 1 2 3) (Arshaft (Rashtft' (1 2 3 4))) gt; '(3 4 1 2)   

There are countless ways to solve this problem, I will leave it to find the best suit for your needs, but remember - always try your Resolve a problem in the case of building blocks already in the disposal, and do not reset the wheel again. Just for fun, here's another way, using the reverse :

  (define (the revshst list) (lets (rev (reverse)) ) (Cons (car rev) (reverse (CDR Reeve))))    

No comments:

Post a Comment