Scheme - find most deeply values nested lists

北城以北 提交于 2019-12-02 05:43:34

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).

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!