How to write List literals (Or: Differences between Arrays and Lists)

Sorry, (still) newbie here: How can I write List literals?

I know how to write Array literals:

[3,8,17]

But if I use that with Data.List.length I get the error “Could not match type List with type Array”. This makes sense, but how can I write Lists in my code?

If this is not possible, I might ask: Why is Array the basic data structure (with literals) and List not?

1 Like

There’s no literal syntax for List, you must use the operators provided: [3,8,17] translates to 3 : 8 : 17 : Nil. Arrays were chosen for the syntax as JavaScript has a built-in type for arrays, and has the same syntax.

3 Likes

There’s also Data.List - purescript-lists - Pursuit, to which you can pass an array (or any data type which has an instance of the Foldable type class), like so:

import Data.List as List

x = List.fromFoldable [3, 8, 17]
3 Likes

Ah, that’s cool. thanks!