I asked several days ago about finding the most deeply nested lists. I implemented the idea that was given, and it works.
But there is another problem: I also need to build a list from the nested list. meaning: If I change (8) and (10 11 12), to leaf1 and leaf2, I need to return: '(ans (leaf1 (8)) (leaf2 (10 11 12)). /ans is a quote
In other words:
my function will get
(1 (2 3) (4 (5) (7 (8) (10 11 12)))))) => the most nested lists are (8) and (10 11 12) => my function will return '(ans (leaf1 (8)) (leaf2 (10 11 12)).
I am trying to find an idea, not an implementation. Thanks.
Yes, this is easy to do. Currently (if I understand correctly) you have a recursive function that descends a tree and uses
consto build a modified copy (in which the most deeply-nested lists are replaced with something). This is a common pattern for tree-recursive functions, but there’s no reason they have to return a value with a similar structure to the input they recur on. For example, you could write a function to walk a tree of numbers and return their sum.In this case it sounds like you probably want to keep the basic structure of your tree-recursive function, but use
consor possiblyappendto build a flat list of the most-deeply-nested-lists you’ve found.I can’t quite tell from your question, but you might also be looking for a way to write a function that returns two separate values: one being the tree with deeply-nested-lists replaced by something else, and the other being the flat list of the replaced bits themselves. In this case you might want to look into the Scheme procedures
valuesandcall-with-values, and maybe the library formlet-valuesif your Scheme has it. See the Schemewiki FAQ here for more info (scroll down).