read
(define (accumulate op initial sequence)
(
if (null? sequence)
initial
(op (car sequence)
(accumulate op initial (cdr sequence)))))

(define fold-right accumulate)
(define (fold
-left op initial sequence)
(define (iter result rest)
(
if (null? rest)
result
(iter (op result (car rest))
(cdr rest))))
(iter initial sequence))

(define nil (list))

(define (reverse_ sequence)
(fold-right solution nil sequence))

(define (reverse_2 sequence)
(fold-left solution2 nil sequence))

(define solution (lambda (x y) (append y (list x))))
(define solution2 (lambda (x y) (cons y x)))

'test result
> (reverse (list 1 2 3 4 5))
(
5 4 3 2 1)
> (reverse_ (list 1 2 3 4 5))
(
5 4 3 2 1)
> (reverse_2 (list 1 2 3 4 5))
(
5 4 3 2 1)
>

Blog Logo

Ki Sung Bae


Published

Image

Gsong's Blog

Developer + Entrepreneur = Entreveloper

Back to Overview