Haskell shying away from probabilistic data structures?

前端 未结 7 873
时光说笑
时光说笑 2021-02-05 07:57

If you search for skips lists implemented in Haskell, you won\'t find many. It is a probabilistic data structure needing a random number generator, meaning that any of these str

7条回答
  •  粉色の甜心
    2021-02-05 08:40

    Skip lists can be implemented purely -- just encapsulate the current seed in the state of the skip list itself.

    data SkipList a = SkipList StdGen (Node a)
    data Node a = ...
    

    This may expose you to some complexity attacks that aren't practical against 'real' skip lists, since you could probe for degenerate insertion orders and replay attacks against the same seed, but it lets you derive the benefits of the structure when adversarial usage is not a problem.

    You could also fall back on unsafePerformIO and a carefully crafted side-effect-oblivious seemingly-pure interface. While admittedly it is not pure internally, the interface gives the appearance of purity.

    That said, many of the classical performance benefits from skiplists come from when they can be implemented non-persistently, and that precludes a functional interface.

提交回复
热议问题