I have just hit another bump in the road along my journey with Scheme. It's probably safe to say my table has had enough of me banging my head into it... I have written a function to find the min and max number within a list for class homework. The logic is sound (i think so...) and everything works fine, however, only the value of the first function call is returned from the (define (iterator aList minNum maxNum)). What I am noticing with the debugger is that after every recursion / function call I see (using DrRacket) that function call being pushed to the stack. Once the recursion happens for the last time and the code jumps down to the return of (list minNum maxNum) it doesn't return what it should, as I can see the values are correct, instead I see the function calls coming off the stack one by one until it gets to the very first one. Thus the initial values which would be the first two values form the list are returned instead. I know the stack is FIFO, however, I am not even trying to push anything to the stack. In theory I just want to call the function again and keep passing values back up... Any guidance on this would be much appreciated.
(define (findMinMax aList)
(define (iterator aList minNum maxNum)
(when(not(null? aList))
(cond
((> minNum (car aList))
(set! minNum (car aList)))
((< maxNum (car aList))
(set! maxNum (car aList))))
(iterator (cdr aList) minNum maxNum))
(list minNum maxNum))
(cond ; setting the first two atoms in a list appropriately to the min and max variables.
((< (car aList) (car(cdr aList)))
(iterator (cdr (cdr aList)) (car aList) (car(cdr aList))))
((> (car aList) (car(cdr aList)))
(iterator (cdr (cdr aList)) (car(cdr aList)) (car aList)))
(else
(iterator (cdr (cdr aList)) (car aList) (car(cdr aList))))))