tags:

views:

801

answers:

3

Hello. I am study the Haskell. I am have the next question :

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

Thank you.

+11  A: 

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.

elder_george
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
@Anton: No, awesome data structures (like finger trees) can be implemented in pure Haskell.
ephemient
jrockway
+3  A: 

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.

Tirpen
Does haskell not language with continuous memory area ?
Anton
@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.
ephemient
+3  A: 

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.

Don Stewart