简体   繁体   中英

Is This Function Tail-Recursive? Scheme

Is the following function tail-recursive? If not, what might I do to modify it?

(define (euclids-alg n1 n2)
 (cond((= n1 0) n2)
      ((= n2 0) n1)
      ((= n1 n2) n1)
      ((> n1 n2) (euclids-alg (- n1 n2) n2))
      ((< n1 n2) (euclids-alg n1 (- n2 n1))))) 

Yes, your function is tail recursive , because the recursive call is in tail position - meaning, it's the last thing it's done after the recursion returns. Take a look at the specification to better understand when we have a valid tail call and when we don't.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM