Be aware that PureScript ships
purescript-lists package) and
purescript-arrays package), and a few other list/array related packages.
Data.List are really strict linked lists, allowing for pattern matching and cool recursive patterns.
Both have their pros and cons, and the choice depends on the task at hand.
Basic Syntax for Creating Lists#
Import the type
List with its
Cons data constructors, and create a simple list:
> import Data.List (List(Nil, Cons)) > Cons 1 (Cons 2 (Cons 3 Nil)) (1 : 2 : 3 : Nil)
Or import the
Cons in its infixr operator form,
> import Data.List (List(Nil), (:)) > 1 : 2 : 3 : Nil (1 : 2 : 3 : Nil)
Note that we import
Cons earlier, even though
: is infix operator alias for
One use of
Cons is to create
singleton function, which takes an element and creates a list with that one, single element:
import Data.List (List(Nil, Cons)) singleton :: ∀ a. a -> List a singleton e = Cons e Nil
Or using the infixr
: operator instead of
import Data.List (List(Nil), (:)) singleton :: ∀ a. a -> List a singleton e = e : Nil