Before I start: YES, this is a homework from college. Before I get told that I’m lazy and evil: this part of the homework was to convert two functions we already had, this one is the 6th.
(define (flatten-list a-list)
(cond ((null? a-list) '())
((list? (car a-list))
(append (flatten-list (car a-list)) (flatten-list (cdr a-list))))
(else (cons (car a-list) (flatten-list (cdr a-list))))))
The function, as you can guess, flattens a list even if it’s nested. My specific problem with the transformation comes in the (list? (car a-list)) condition, in which I’m doing two recursive calls. I already did fibonacci, which I can do by just having two "acummulators" on the tail recursion. However, my mind is not trained in this yet to know how it should go.
I would appreciate if I was given hints and not the result. Thanks!
Here’s my solution:
Tail-recrusive functions require that they never return, and thus you can’t use stack for storing your program’s state. Instead, you use function arguments to pass the state between function calls. Therefore, we need to determine how to maintain the state. Because the result of our function is
list?, it’s meaningful to grow anemptylist; we’re usingaccfor this purpose. You can see how it works inelsebranch above. But we should be able to process nested lists. And while we’re going deeper, we should keep the rest elements of the nested list for further processing. Sample list:(list 1 (list 2 3) 4 5)Until
(list 2 3)we have already added1to accumulator. Since we can’t use stack, we need some other place to store the rest elements of the list. And this place is thelstargument, which contains elements of the original list to be flattened. We can justappendthelstto the rest elements(cdr (list 2 3))which are(list 3), and proceed with the list’s head we stumbled upon while flattening, i. e. (car (list 2 3)) which is just2. Now,(and (list? lst-interm) (not (null? lst-interm)))succeeds becauseflat-dois called this way:and the condition triggers this code:
flat-doagain is called this way: (flat-do (list 1) 2 (list 3 4 5))The condition
(not (list? 2))now succeeds and the code(flat-do (cons 2 1) empty (list 3 4 5))is evaluated.The rest processing is done with
elsebranch untillstisnull?andreverseis performed onacc. Function then returns the reversed accumulator.