I have to create a recursive Scheme function for my programming class that will take all the odd-numbered elements of a list, and then return them in reversed order.
I have a function for reversing a list, and another function for getting the odd-numbered elements, but can't figure out how to combine the two into a new function, as they both are recursive.
It has to be one function that doesn't call any functions other than itself. It can not call odd
or reverse
, and it has to have the same functionality as calling both would have.
Odd Function:
(define (odd lst)
(if (null? lst)
'()
(cons (car lst)
(odd (cddr lst)))))
Reverse Function:
(define (reverse lst)
(if (null? lst)
'()
(append (reverse (cdr lst))
(list (car lst)))))
Any help would be appreciated!