本文介绍了在Haskell成长阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要在Haskell实现以下(必须)算法:

I have the following (imperative) algorithm that I want to implement in Haskell:

给定对的序列[(E0,S0),(E1,S1),(E2,S2),...,(烯,SN)],其中两个e和S部分是天然号码不一定不同,在这个序列中的每个时间步长的一个元素是随机选择的,让我们说(EI,SI),和设在的值(EI,SI),一个新的元件构建,并加入到该序列。

Given a sequence of pairs [(e0,s0), (e1,s1), (e2,s2),...,(en,sn)], where both "e" and "s" parts are natural numbers not necessarily different, at each time step one element of this sequence is randomly selected, let's say (ei,si), and based in the values of (ei,si), a new element is built and added to the sequence.

如何在Haskell有效地实现这一点?需要随机访问将使坏的名单,而需要在每次追加一个元素将使坏阵列,据我所知。

How can I implement this efficiently in Haskell? The need for random access would make it bad for lists, while the need for appending one element at a time would make it bad for arrays, as far as I know.

先谢谢了。

推荐答案

我建议要么使用在data.set Data.Sequence ,这取决于你需要它为了什么。特别是后者提供了对数索引查找(而不是对列表线性)和O(1)在任一端追加

I suggest using either Data.Set or Data.Sequence, depending on what you're needing it for. The latter in particular provides you with logarithmic index lookup (as opposed to linear for lists) and O(1) appending on either end.

这篇关于在Haskell成长阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:20