2.39

练习 2.39 基于练习2.38的fold-right和fold-left完成reverse(练习2.18)下面的定义:

(define (reverse sequence)
    (fold-right (lambda (x y) <??>) nil sequence))

(define (reverse sequence)
    (fold-left (lambda (x y) <??>) nil sequence))

(reverse (list 1 4 9 16 25))
(define (reverse1 sequence)
    (fold-right (lambda (x y) (append y (list x))) '() sequence))

(reverse1 (list 1 4 9 16 25))
(define (reverse2 sequence)
    (fold-left (lambda (x y) (cons y x)) '() sequence))

(reverse2 (list 1 4 9 16 25))

results matching ""

    No results matching ""