我目前正在通過Hailperin,Kaiser和Knight的計劃書「Concrete Abstractions」。我已經編寫了如下所示的相同算法的遞歸和迭代版本。該算法用於對數量或事物進行n次操作。所以,一般來說,一起復制(操作n的東西)。例如,我可以找到3個像這樣的立方體,一起復制(* 3 3)或3個平方複製(* 2 3)。對數時間計劃算法
遞歸版本:
(define together-copies-of-linrec
(lambda (combine quantity thing)
(define together-iter
(lambda (combine start thing)
(if (= start quantity)
thing
(combine (together-iter combine (+ start 1) thing)
thing))))
(together-iter combine 1 thing)))
迭代版本:
(define together-copies-of-linit
(lambda (combine quantity thing)
(define together-iter
(lambda (combine start newthing)
(if (= start quantity)
newthing
(together-iter combine (+ start 1) (combine newthing thing)))))
(together-iter combine 1 thing)))
現在,我需要寫這個算法的對數時間的版本,但我真的不知道從哪裏開始。在這種情況下,我看不出如何減少每個實例的操作使其成爲對數時間。