(lambda(elem ls)
(cond
((null? ls) #f)
((eq? elem (car ls)) #t)
(else (member- elem (cdr ls))))))
cps变换:
(define lot
(lambda(ls)
(not (null? ls))))
(define member&
(lambda (elem ls cont)
(cond
((null? ls) (cont '()))
((eq? elem (car ls)) (member& elem (cdr ls) (lambda (res) (cont (cons elem res)))))
(else (member& elem (cdr ls) (lambda (res) (cont res)))))))
cps变换:
(define member-cps
(lambda(elem ls k)
(cond
((null? ls) (k #f))
((eq? elem (car ls)) (k #t))
(else (member-cps elem (cdr ls) k)))))
(member-cps 'a '(b c a) display) 返回 #t
=======================================================================================
(define lat?
(lambda (ls)
(cond
((null? ls) #t)
((atom? (car ls)) (lat? (cdr ls)))
(else #f))))
cps变换:
(define col
(lambda (ls)
(null? ls)))
(define lat?&co
(lambda (ls cont)
(cond
((null? ls) (cont '()))
((atom? (car ls)) (lat?&co (cdr ls) (lambda (res) (cont res))))
(else (lat?&co (cdr ls) (lambda (res) (cont (cons (car ls) res))))))))
======================================================================================
(define fact
(lambda (num)
(cond
((zero? num) 1)
(else (* num (fact (- num 1)))))))
cps变换:
(define col
(lambda (num)
(display num)))
(define fact&co
(lambda (num col)
(cond
((zero? num) (col 1))
(else (fact&co (- num 1) (lambda (res) (col (* num res))))))))
测试:
(member- 1 '(3 4 6 8 9)) 返回#f
(member- 1 '(3 4 6 8 9 1)) 返回#t
(member& 1 '(3 4 6 8 9) lot) 返回#f
(member& 1 '(3 4 6 8 9 1) lot) 返回#t
(lat?&co '(1 2 (3) 4) col) 返回#f
(lat?&co '(1 2 3 4) col) 返回#t
(fact 4) 返回24
(fact&co 4 col) 返回24