Friday 15 March 2013

Is this scheme code tail recursive? -


Edit: Thanks for everyone I'm new to language (just started experimenting it two days ago) , So I'm unfamiliar with the Condoles. If I have time, then I can write it again, but I wanted to make sure that I have basic logic properly. Thanks again!

My job is to create a tail-recursive function, which removes the nth element from the list, 1 & lt; = N & lt; = Listlength, with only two parameters, list x and element n, therefore, (1) (ABCD) will be returned (BCD). I have written the following, and I need some assurance that this is really tail recursive. The only thing I am fuzzy if the recursive call can be nested inside the IF statement.

  (define nx); If n is 1, then simply cdr (if (and not (not (list (car X)) () = (1 =) 1) (CDR X); If the car is not a list, make it one, and call again (If not (list (car X)) (Remove (- N1) (opposition (list (car X)) (CDR X)); if n! = 1, insert the cadre into the list in the car; Other, add the list to the car Cddr (cddr x) (cddr x) (cddr x) (cddr x) (cddr x) (cddr x));))))))) < / Pre>  

Yes, the process is tail-recursive, which means: wherever the recursive The call is done, this is the last thing that happens in that particular branch of execution, in which nothing happens after the recurring return - so we say that the recursive call is in tail position

It can be clearly seen that we instead rewrite the process using cond nested if s, you will see that the execution Every branch leads to a base position or a recursive case , And all are in a position recursive call Tail:

  (define (remove Nx); Base case # 1 (code (and (not list (car X)) (= N1); Returning from base case, this is not a recursive (CDR X); recursive case # 1 ((not list ( The car is in the position of the tail (remove (- 1) (opposition (list of car x)) (CDR x)); Recursive case # 2 ((not (= N1)); base case # 2 (and returning from the base case); Recurring call is in tail position (Remove (N1) (opposition (Annexure (Car X) (list cadre x))); Not recurrent   

for a more technical explanation of a if / as a result of alternate parts> specially It can be considered tail-recursive, a noticeable algorithm language plan on section 3.5 of the current draft of the revised ^ 7 report - language specification, is here on the PDF file (briefly the same idea applies R5RS, it is just that they R7RS has been explained in more detail). े:

If one of the following expressions is in reference to the tail, then the sub-expressions are shown as "pseudo-expression"? In the context of tail

...

(If one ?? Expression? An ¨ ¨ ¨ anl expression expression ???? < / S> ???????>

(If a ?? expression ?? ¨¨ ¨ailail expression) ?? ©

No comments:

Post a Comment