1

I am studying Haskell. I have the next question:

The List type is a basic type in Haskell. Array in Haskell is based on lists. This is the list of indices [Ix a] and function presents by table - list of pairs [(Ix a,Value)]. Why is Array faster than lists, if inside it use lists?

4
  • 4
    Faster for what operations? How do you know it's faster? Commented Oct 15, 2009 at 13:20
  • For instance, add to end of (array/list) , or get i element. Commented Oct 15, 2009 at 16:14
  • Both Data.Array and Data.List require "copy everything" to add to the end of an (array/list). Commented Oct 15, 2009 at 17:54
  • Yes, Data.Array.MArray has fixed bounds. If you wish to change the number of elements, you must create a new array. Commented Oct 15, 2009 at 20:55

3 Answers 3

21

I am afraid you're wrong.

There're several implementations of arrays in Haskell, but as far as I understand, all of them are implemented on top of contiguous memory arrays. Because of this there's a possibility of O(1) access to element for reading.

Similarity between arrays and lists exists only on operation set level.

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

3 Comments

Therefore, do not design a faster data structure on haskell. All of good "Data Structure" writen in other languages. May be С\С++. Am i right ?
@Anton: No, awesome data structures (like finger trees) can be implemented in pure Haskell.
Recommended reading: books.google.com/…
6

Arrays are not based on lists, they are implemented similarly to other programming languages, with a continuous memory area.

The most common way to create them, the array function takes a list of index, value pairs as an argument and when you print them they are shown like such a list as well. This is because arrays in Haskell can be indexed not just by integers but by anything that implements the Ix typeclass, for example (Int, Int)-pairs, Booleans, Char's and many other versions. So the [(index, value)] representation is really the only sensible, consistent way to display an array as general as the ones in Haskell.

2 Comments

Does haskell not language with continuous memory area ?
@Anton: The Haskell'98 language specification mandates the existence of Array module (and associated classes, types, and functions). It says nothing about the implementation aside from "a programmer may reasonably expect rapid access to the components", but this is generally taken an interface to continuous random access memory.
6

Arrays are not based on lists. Lists are a regular, recursive data type:

data [] a = [] | a : [a]

While the various array libraries, such as uvector, array, vector, carray, hmatrix, use low level read and write operations to present an interface to arrays. There's no similarity, other than that both data structures can represent sequences, though with different complexity.

Comments

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.