我发现下面的执行二进制搜索的方案:是作为尾递归goto指令吗?
(define (binary-search value vector)
(let helper ((low 0)
(high (- (vector-length vector) 1)))
(if (< high low)
#f
(let ((middle (quotient (+ low high) 2)))
(cond ((> (vector-ref vector middle) value)
(helper low (- middle 1)))
((< (vector-ref vector middle) value)
(helper (+ middle 1) high))
(else middle))))))
根据它所在评论中说,上述函数使用尾递归调用的帮助功能。我想知道这是否像GOTO指令一样工作,因为我没有看到对二进制搜索函数有适当的“递归”调用。
在这种情况下,说它适合像goto指令吗?
谢谢,那么你是说真的在这里进行递归的函数是对辅助函数的调用吗? – Layla 2014-11-03 03:05:22
@Layla是的。 :-) – 2014-11-03 04:05:34