Commit 65742bf8 authored by Fernando Basso's avatar Fernando Basso
Browse files

purs: Add notes on Data.List Cons data constructor

This addition required some changes in the structure and description of
the “functions explained” section, like, changing the directory name,
titles, and intro notes.
parent b28f3950
Loading
Loading
Loading
Loading
+37 −0
Original line number Diff line number Diff line
---
title: Cons, Data.List | PureScript
description: Ins and outs of PureScript `Cons' data constructor with practical examples.
---

# Cons (Data.List)

`Cons` is data constructor from `Data.List` `List` type.
It conses (short for *construct*, from Lisp parlance) a list by prepending an element to the *head* of a list.

It is not like `Array.prototype.push` in ECMAScript because it does not modify the existing structure, but returns a new list instead.

## Manually Create a List

```text
> import Data.List (List(Cons, Nil))

> Cons 1 Nil
(1 : Nil)

> Cons 1 (Cons 2 Nil)
(1 : 2 : Nil)

> Cons 1 (Cons 2 (Cons 3 Nil))
(1 : 2 : 3 : Nil)
```

Note that even though we use `Cons`, it prints in the REPL with `:`.

In PureScript, `Cons` is aliased to the `infixr` operator `:`. We use it like this:

```text
> import Data.List (List(Nil), (:)) 

> 1 : 2 : 3 : Nil
(1 : 2 : 3 : Nil)
```
+7 −3
Original line number Diff line number Diff line
@@ -3,13 +3,15 @@ title: Functions Explained | PureScript
description: Let's scrutinize type signature, implementation, evaluation and use of several functions with practical examples.
---

# PureScript Functions Explained
# PureScript Explained

In this section we'll study and understand the type signature of several functions, sometimes considering alternate parenthesizing of those type signatures. We'll also study the possible implementations (many of them simplified), together with their usage and evaluation steps.
In this section we'll study and understand type signature, functions, types, data constructors, etc.
We'll also study the possible implementations (many of them simplified), together with their usage and evaluation steps.

For brevity, we'll colloquially say things like “this function takes two parameters”, but that will always mean (unless explicitly noted) that it is a function that takes one parameter, and return a function that takes another parameter, that then returns the final result.

Also, sometimes we will use a short name like `f`, `g`, `fn`, etc. instead of longer names like `const`,  `append`, `identity` or `compare`. The goal is to make evaluation steps shorter in the text.
Also, sometimes we will use a short name like `f`, `g`, `fn`, etc. instead of longer names like `const`,  `append`, `identity` or `compare`.
The goal is to make evaluation steps shorter in the text.

```{toctree}
---
@@ -21,4 +23,6 @@ caption: PureScript
const.md
apply.md
apply-flipped.md

data-list-cons.md
```
Loading