全民Scheme(2):来自星星的你
一门编程语言,假设不能对你思考编程的方式产生影响。就不值得去学习。—— Alan Perlis
(define rember*
(lambda (a list)
(cond
((null? list) '())
((atom? (car list)) (cond
((eq? a (car list)) (rember* a (cdr list)))
(else (cons (car list) (rember* a (cdr list))))))
(else (cons (rember* a (car list)) (rember* a (cdr list)))))))
(rember* 'cup '((coffee) cup ((tea) cup) (and (hick)) cup))
(define insertR*
(lambda (new old l)
(cond
((null? l) '())
((atom?
(car l)) (cond
((eq?
(car l) old) (cons old (cons new (cdr l))))
(else (cons (car l) (insertR* new old (cdr l))))))
(else (cons (insertR* new old (car l)) (insertR* new old (cdr l)))))))
(insertR* 'roast 'chuck '((how much (wood))
could
((a (wood) chuck))
(((chuck)))
(if (a) ((wood chuck)))
could chuck wood))
(define occur*
(lambda (a l)
(cond
((null?
l) 0)
((atom?
(car l)) (cond ((eq? a (car l)) (+ 1 (occur* a (cdr l))))
(else (occur* a (cdr l)))))
(else (+ (occur* a (car l)) (occur* a (cdr l)))))))
(occur* 'banana '((banana) (split ((((banana ice)))
(cream (banana))
sherbet))
(banana)
(bread)
(banana brandy)))
(define subst*
(lambda (new old l)
(cond
((null? l) '())
((atom? (car l)) (cond
((eq? old (car l)) (cons new (subst* new old (cdr l))))
(else (cons (car l) (subst* new old (cdr l))))))
(else (cons (subst* new old (car l)) (subst* new old (cdr l)))))))
(subst* 'orange 'banana '((banana) (split ((((banana ice)))
(cream (banana))
sherbet))
(banana)
(bread)
(banana brandy)))
(define insertL*
(lambda (new old l)
(cond
((null? l) '())
((atom?
(car l)) (cond
((eq?
old (car l)) (cons new (cons old (insertL* new old (cdr l)))))
(else (cons (car l) (insertL* new old (cdr l))))
))
(else (cons (insertL* new old (car l)) (insertL* new old (cdr l)))))))
(insertL* 'pecker 'chuck '((how much (wood))
could
((a (wood) chuck))
(((chunck)))
(if (a) ((wood chuck)))
(could chuck wood)))
(define member*
(lambda (a l)
(cond
((null? l) #f)
((atom? (car l)) (cond
((eq?
(car l) a) #t)
(else (member* a (cdr l)))))
(else (or (member* a (car l))
(member* a (cdr l)))))))
(member* 'chips '((potato) (chips ((with) fish)(chips))))