A Naive way to implement Reverse


(define (reverse l)
    (if (null? l)
        '()
        (cons (reverse (cdr l)) (car l))
    )
)
 

But that produces


==> (reverse '(a b c d e ))

(((((() . e) . d) . c) . b) . a)
 

A Better way to implement reverse

(define (last x)
        (if (null? (cdr x))
             (car x)
             (last (cdr x))
        )
)

 (define (allbutlast x)
       (if (null? (cdr x))
            `()
            (cons  (car x) (allbutlast (cdr x)))
        )
)

(define (reverse x)
        (if (null? x)
             `()
             (cons (last x) (reverse (allbutlast x)))
        )
)
 

A Better Still reverse

(define (reverse l)
    (if (null? l)
        '()
        (if (null? (cdr l))
            l
            (if (null? (cdr (cdr l)))
                (list (car (cdr l)) (car l))
                (append (reverse (cdr l)) (list (car l)))
             )
        )
    )
)
 
 

An Even Better way to Implement reverse

(define (reverse l)
    (if (null? l)
        '()
        (append (reverse (cdr l)) (list (car l)))
    )
)