I was wondering how to reverse a list using only basic operations such as cons, first, rest, empty?, etc.
No helper functions or accumulators allowed, and the funct
(define (reverse x)
(let loop ((x x) (y '()))
(if (null? x)
y
(let ((temp (cdr x)))
(set-cdr! x y)
(loop temp x))))))
Really one of the few ways to do it efficiently. But still sort of a helper procedure.
Other way, but not tail-recursive, and if the append doesn't use a set-cdr! it's really unusable for large lists.
(define (reverse L)
(if (null? l)
'()
(append (reverse (cdr L)) (list (car L)))))