Scheme - find most deeply values nested lists

后端 未结 1 1585
情深已故
情深已故 2021-01-27 04:36

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 b

相关标签:
1条回答
  • 2021-01-27 05:11

    Yes, this is easy to do. Currently (if I understand correctly) you have a recursive function that descends a tree and uses cons to 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 cons or possibly append to 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 values and call-with-values, and maybe the library form let-values if your Scheme has it. See the Schemewiki FAQ here for more info (scroll down).

    0 讨论(0)
提交回复
热议问题