For my programming languages class I\'m supposed to write a function in Scheme to reverse a list without using the pre-made reverse function. So far what I got was
You are half way there. The order of the elements in your result is correct, only the structure needs fixing.
What you want is to perform this transformation:
(((() . c) . b) . a) ; input
--------------------
(((() . c) . b) . a) () ; trans-
((() . c) . b) (a) ; for-
(() . c) (b a) ; mation
() (c b a) ; steps
--------------------
(c b a) ; result
This is easy to code. The car
and cdr
of the interim value are immediately available to us. At each step, the next interim-result is constructed by (cons (cdr interim-value) interim-result)
, and interim-result
starts up as an empty list, because this is what we construct here - a list:
(define (transform-rev input)
(let step ( (interim-value input) ; initial set-up of
(interim-result '() ) ) ; the two loop variables
(if (null? interim-value)
interim-result ; return it in the end, or else
(step (car interim-value) ; go on with the next interim value
(cons ; and the next interim result
(... what goes here? ...)
interim-result )))))
interim-result
serves as an accumulator. This is what's known as "accumulator technique". step
represents a loop's step coded with "named-let" syntax.
So overall reverse is
(define (my-reverse lst)
(transform-rev
(reverseList lst)))
Can you tweak transform-rev
so that it is able to accept the original list as an input, and thus skip the reverseList
call? You only need to change the data-access parts, i.e. how you get the next interim value, and what you add into the interim result.