药剂反向连续 [英] Reverse concat in Elixir
问题描述
Elixir(或Erlang)中是否有标准库函数将其他列表前面的列表的倒数连接起来?基本上我正在Scala中找到相当于 reverse _ :::
的等价物。
Is there a standard library function in Elixir (or Erlang) to concatenate the reverse of a list in front of some other list? Basically I'm looking for an equivalent of reverse_:::
in Scala.
理由是它很方便当在列表上实现尾递归算法时。在递归期间,您可以保留一些元素,以便稍后将它们添加到累加器列表的前端。最后,您可以一次性将它们反向连接到组合列表的剩余部分(这应该是非常有效的)。
The rationale is that it's handy when implementing a tail-recursive algorithm on a list. During recursion, you hold on to some of the elements for later by adding them onto the front of an accumulator list. In the end you can reverse-concat them onto the remainder of the assembled list in one go (which should be pretty efficient).
推荐答案
p>您可以使用 在Erlang中执行反向和并发列表:reverse / 2
。
You can do a reverse and concat in Erlang by using lists:reverse/2
.
我认为文档说明和示例足够清楚:
I think the documentation explanation and example are clear enough:
reverse(List1,Tail) - >列表2
。
以相反的顺序返回一个包含 List1
中的元素的列表,
尾尾部
附加。
Returns a list with the elements in List1
in reverse order, with the
tail Tail
appended.
> lists:reverse([1, 2, 3, 4], [a, b, c]).
[4,3,2,1,a,b,c]
这篇关于药剂反向连续的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!