How to do this length≤1 more than once?
Asked Answered
N

1

2

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)
    (mk-length mk-length))
  (lambda (mk-length)
   (lambda (l)
    (cond
      ((null? l) 0)
      (else (add1 
           ((mk-length eternity)
            (cdr l))))))))
 l)

where l is (apples) and eternity is as follows:

(define eternity 
  (lambda (x)
    (eternity x)))

Page 166 (4th ed.) states that:

When we apply mk-length once, we get length≤1

And then

Could we do this more than once?

But I do not know how to do this for getting length≤2?

Nganngc answered 24/9, 2012 at 11:22 Comment(1)
I think this answer of mine answers this question, for length≤2 specifically (i.e. only 2, not more).Nestling
C
3

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.

Cronyism answered 25/9, 2012 at 22:59 Comment(1)
with mk-length instead of eternity this will be length≤∞, not length≤2.Nestling

© 2022 - 2024 — McMap. All rights reserved.