In the book The little schemer, we find this function that only supports lists with length smaller than or equal to 1:
(((lambda (mk-length) ; A.
(mk-length mk-length))
(lambda (mk-length)
(lambda (l)
(cond
((null? l ) 0)
(else (add1 ((mk-length eternity ) (cdr l))))))))
'(1))
I want to study step by step, and want to write the similar function that supports only lists of length smaller than or equal to 2.
Please, don't answer this question by offering code like:
(((lambda (mk-length) ; B.
(mk-length mk-length))
(lambda (mk-length)
(lambda (l)
(cond
((null? l) 0 )
(else (add1((mk-length mk-length) (cdr l))))))))
'(a b c d))
because this function supports any length.
And I already know how to write function like this:
(((lambda (mk-length) ; C.
(mk-length
(mk-length (mk-length eternity))))
(lambda (length)
(lambda (l)
(cond
((null? l) 0)
(else (add1 (length (cdr l))))))))
'(1 2)) ;;
to achieve my goal. But this code is more than one step away from the first snippet.
Maybe, I should not change:
(lambda (mk-length) ; D.
(mk-length mk-length)
(mk-length eternity )
to(mk-length mk-length)
,can achieve the any length. It is hard to think, so I want to implement a just<=2
version. on 1th code's base – Penelopalength≤∞
, notlength≤2
. – Twomey