17

In PureScript, how does List differ from Array?

What are the reasons to use one over the other?

sdgfsdh
  • 33,689
  • 26
  • 132
  • 245

1 Answers1

23

The representation for Array is a JavaScript array, whereas List is implemented as a cons (linked) list.

Lists have better performance characteristics when being built up item-by-item, or iterated over by taking an item from front each time - basically List has O(1) cons and uncons, vs O(n) for Array.

Take a look at the documentation for Array and List on Pursuit for more information about the running time of various operations.

gb.
  • 4,629
  • 1
  • 20
  • 19
  • 15
    It is worth noting that the List notation in Purescript (for example `[1,2,3]`) produces an `Array` of the given type and not a `List`. – babelchips Aug 04 '17 at 15:46
  • 10
    To add further, ```1 : 2 : 3 : Nil``` will give you the required ```List``` of elements ```[1,2,3]``` – kishlaya Jun 07 '18 at 11:07
  • Also, `import Data.List (range)` then `range 1 3` also produces `(1 : 2 : 3 : Nil)`, which is a list, and not an array. – Fernando Basso Jan 23 '22 at 10:40