This started as a misinterpretation of exercise 2.29 in SICP and became a personal curiosity. It seems simple enough I feel embarrassed I'm having such trouble finding a solution that works for all cases. Obviously I know how to count the nodes on a tree and how to enumerate the values in every node into one list, but neither algorithm is helping me filter for the same index in every node.
Given any binary tree represented as a nested list where each node holds either one or two integers, there should be one function that returns a list of the first value in every node and one that returns the second value in every node (keeping in mind not all nodes will have a second value).
Here's what I have so far with a simple test case:
(define (first-values tree)
(cond ((null? tree) '())
((not (pair? tree)) (list tree))
(else (cons (car (first-values (car tree)))
(first-values (car (cdr tree)))))))
(define (second-values tree)
(cond ((null? tree) '())
((not (pair? tree)) (list tree))
(else (cons (cdr (second-values (car tree)))
(second-values (car (cdr tree)))))))
(define (make-tree left right)
(list left right))
(define test (make-tree
(make-tree 2 5)
(make-tree (make-tree 4 10) (make-tree 6 20))))
test
(first-values test)
(second-values test)
And the result:
((2 5) ((4 10) (6 20)))
(2 4 6 20)
((5) (10) () 20)
As you can see, the first function won't filter values in sublists and the second leaves extraneous sublists I would need to filter out with an enumerate function. I really tried every variation of car and cdr I could think of and this was the closest I came. It clearly shows I still don't fully understand working with list structure even though it hasn't been an issue with simpler examples.
(For reference I'm using R5RS)