use*_*875 0 haskell tuples list modulo
我想从列表选择的元素,[1,2],[3,4],[5,6]]一旦第一,比第二,不是再次第一等等.我想我可以使用zip在对前面添加一个计数器并使用modulo来选择部件,现在我的列表如下所示:
let a = [(0,[1,2]),(1,[3,4]),(2,[5,6]),(3,[7,8]),(4,[9,10])]
Run Code Online (Sandbox Code Playgroud)
但是我现在如何选择元素呢?
伪代码将是
for each tuple in list:
first part of tuple is the selector, second part is the pair
if selector mod 2 : choose pair[0] else choose pair[1]
Run Code Online (Sandbox Code Playgroud)
列表a的输出应为:1,4,5,7,9
也许:
> zipWith (!!) [[1,2],[3,4],[5,6],[7,8],[9,10]] (cycle [0,1])
[1,4,5,8,9]
Run Code Online (Sandbox Code Playgroud)
如果你知道你正在处理长度为2的列表,你可能应该使用对.
> zipWith ($) (cycle [fst, snd]) [(1,2),(3,4),(5,6),(7,8),(9,10)]
[1,4,5,8,9]
Run Code Online (Sandbox Code Playgroud)