列表中所有匹配元素的位置
我正在尝试在 Common Lisp 中编写一个类似于内置位置函数的函数,该函数返回大海捞针中与针匹配的所有元素的位置列表,而不是仅返回第一个。我提出了一些可能的解决方案(例如,在该位置上使用 cdr-from 函数递归搜索下一个元素并将结果添加到前一个位置),但到目前为止我还没有提出任何方法显得特别优雅。
任何人都可以建议什么是解决这个问题的最佳方法,因为我目前正在挣扎。
I'm trying to write a function in Common Lisp similar to the built in position function, that returns a list of the positions of all elements in the haystack that match the needle, as opposed to just the first. I've come up with a few possible solutions (for example recursively searching for the next element using a cdr-from function on the position and adding the result to the previous position) but none of the approaches I've come up with so far seem particularly elegant.
Can anyone suggest what would be the best way of approaching this, as I'm currently struggling.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
解决该问题的明显方法是依次查看列表中的每个元素,每次比较等于针时,将其位置收集到输出列表中。在这种情况下获取位置非常容易,因为我们从 haystack 的开头开始; 我们可以使用一个变量来从 0 开始计算当前位置。
因此,如果我们用句子中,我们会说类似“找到大海捞针在大海捞针中的所有位置,对于大海捞针中的每个元素,从0开始的位置,当该元素等于针时,收集该位置。”
当您想要进行迭代处理时,LOOP 工具基本上是正确的选择。尽管它的语法对于正式描述来说很复杂, 经过一些经验你几乎可以将算法的英文描述放在LOOP 的主体,它将起作用。
The obvious way to solve the problem is just to look at each element of the list in turn, and each time one compares as equal to the needle collect its position into an output list. Getting the position is very easy in this case, because we are starting from the beginning of haystack; we can use a variable to count the current position starting from 0.
So if we describe the full algorithm in a sentence, we'd say something like "to find all the positions of a needle in a haystack, for each element in the haystack, and the position starting from 0, when the element is equal to the needle, collect the position."
The LOOP facility is basically the right thing to break out when you want to do iterative processing. Even though its syntax is complicated to describe formally, after some experience you can pretty much just put the English-language description of the algorithm in the body of LOOP and it will work.
对此持保留态度(并确保事先加载Alexandria):
也就是说,它确实具有处理任意序列的优点:
Take this one with a grain of salt (and be sure to load Alexandria beforehand):
That said, it does have the advantage of working on arbitrary sequences:
如果您想要一个递归函数,而不是基于
(loop ...)
的函数,您可以使用类似以下内容的函数:If you want a recursive function, rather than a
(loop ...)
based one, you could use something like:这是另一种(不一定更好)的方法。
Here's another (not necessarily better) way to do it.