How to do this length≤1 more than once?

前端 未结 1 923
猫巷女王i
猫巷女王i 2021-01-22 00:09

I\'ve spent a day reading page 166\'s length≤1 in the book The Little Schemer; there\'s the following code:

(((lambda (mk-length)
          


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-22 00:55

    Suppose l is (apples oranges), then it will evaluate like this (note that mk-length is bound to the the (lambda (mk-length) ...) function itself:

    (cond ((null? l) 0) 
          (else (add1 ((mk-length eternity) (cdr l)))))
    ==>
    (add1 ((mk-length eternity) '(oranges)))
    ==>
    (add1 ((lambda (l) (cond ((null? l) 0
                              (else (add1 ((eternity eternity) (cdr l))))))))
    ==>
    (add1 (add1 ((eternity eternity) '())))
    

    So here, after two steps, eternity ends up being applied, but what we want is for it to call mk-length. So in the original function, if we replace eternity by mk-length, then the last step I wrote will contain (mk-length mk-length) instead of (eternity eternity), allowing the computation to proceed.

    0 讨论(0)
提交回复
热议问题