3

What is the main difference between Data.Sequence / Seq a and Data.IntMap / IntMap a in Haskell? From everything I've read, they both offer a fast cons function, fast random access, and fast random update. What's the difference?

For context, I need all of these (fast cons, fast random access, and fast random update).

Which should I choose?

1
  • 5
    What implementation do you have for cons for IntMap? IntMap indexes don't "shift over." Commented Aug 7, 2021 at 13:36

1 Answer 1

3

A Seq stores something for every index between 0 and some number. An IntMap stores something for some but not all Int values. This is a big conceptual difference with obvious consequences for space usage—if your data are sparse, you probably don't want a dense representation. Cons and snoc are somewhat cheaper for Seq than for IntMap, I'd expect, though they have different meanings, while random access is significantly slower. It sounds like you might be interested in what Okasaki calls a "random access list", but between Seq and IntMap, you'd best analyze your specific algorithm and benchmark.

Sign up to request clarification or add additional context in comments.

2 Comments

Random access into Seq is going to be somewhat slower than IntMap on most occasions, but it's still sub-linear. That's not nothing.
@Carl, for sure. Louis Wasserman's point that cons and snoc do different things is probably more important in the end.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.