Skip to content

Commit

Permalink
Add lists concept
Browse files Browse the repository at this point in the history
  • Loading branch information
meatball133 committed Sep 30, 2024
1 parent 8f8817f commit be6635e
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 47 deletions.
26 changes: 13 additions & 13 deletions concepts/lists/about.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,32 @@ There are several common Prelude functions for lists:
- [`head`][head] returns the _head_ of a list -- the _first_ item in a list.
- [`tail`][tail] returns the _tail_ of the list -- the list _minus_ the _first_ item.
- [`length`][length] returns the number items in the list.
- [`elem`][in] returns a boolean value indicating whether the item is an element in the list.
- [`elem`][elem] returns a boolean value indicating whether the item is an element in the list.

There is also the [`List` module][list].
There is also the [`Data.List` module][list].

Lists can only contain one data type.
## List types

In Haskell, lists are **homogenous**.
This means that all elements of a list have the same type.
When you try to put elements of different types into the same list, you will get a type error.

```haskell
list = [1, "string"]
-- Error: No instance for (Num String) arising from the literal ‘1’
```

## Type annotation

The type annotation of a list is `[a]` where a is the type which the lists holds, for example `String` or `Int`.

``` haskell
a :: [Int]
a = [1, 2, 3]
```

[enum]: https://hexdocs.pm/elixir/Enum.html
[enum-protocol]: https://hexdocs.pm/elixir/Enumerable.html
[hd]: https://hexdocs.pm/elixir/Kernel.html#hd/1
[in]: https://hexdocs.pm/elixir/Kernel.html#in/2
[length]: https://hexdocs.pm/elixir/Kernel.html#length/1
[list]: https://hexdocs.pm/elixir/List.html
[stream]: https://hexdocs.pm/elixir/Stream.html
[tl]: https://hexdocs.pm/elixir/Kernel.html#tl/1
[prelude]: https://hackage.haskell.org/package/base/docs/Prelude.html
[list]: https://hackage.haskell.org/package/base/docs/Data-List.html
[head]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:head
[tail]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:tail
[elem]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:elem
[length]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:length
[linked-list-wiki]: https://en.wikipedia.org/wiki/Linked_list
28 changes: 14 additions & 14 deletions concepts/lists/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Head-tail notation can be used to append items to a list.
list = [2, 1]

[3, 2, 1] == 3 : list
-- true
-- -> True
```

Appending elements to a list during iteration is considered an anti-pattern.
Expand All @@ -56,32 +56,32 @@ There are several common Prelude functions for lists:
- [`head`][head] returns the _head_ of a list -- the _first_ item in a list.
- [`tail`][tail] returns the _tail_ of the list -- the list _minus_ the _first_ item.
- [`length`][length] returns the number items in the list.
- [`elem`][in] returns a boolean value indicating whether the item is an element in the list.
- [`elem`][elem] returns a boolean value indicating whether the item is an element in the list.

There is also the [`List` module][list].
There is also the [`Data.List` module][list].

Lists can only contain one data type.
## List types

In Haskell, lists are **homogenous**.
This means that all elements of a list have the same type.
When you try to put elements of different types into the same list, you will get a type error.

```haskell
list = [1, "string"]
-- Error: No instance for (Num String) arising from the literal ‘1’
```

## Type annotation

The type annotation of a list is `[a]` where a is the type which the lists holds, for example `String` or `Int`.

``` haskell
a :: [Int]
a = [1, 2, 3]
```

[enum]: https://hexdocs.pm/elixir/Enum.html
[enum-protocol]: https://hexdocs.pm/elixir/Enumerable.html
[hd]: https://hexdocs.pm/elixir/Kernel.html#hd/1
[in]: https://hexdocs.pm/elixir/Kernel.html#in/2
[length]: https://hexdocs.pm/elixir/Kernel.html#length/1
[list]: https://hexdocs.pm/elixir/List.html
[stream]: https://hexdocs.pm/elixir/Stream.html
[tl]: https://hexdocs.pm/elixir/Kernel.html#tl/1
[prelude]: https://hackage.haskell.org/package/base/docs/Prelude.html
[list]: https://hackage.haskell.org/package/base/docs/Data-List.html
[head]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:head
[tail]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:tail
[elem]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:elem
[length]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:length
[linked-list-wiki]: https://en.wikipedia.org/wiki/Linked_list
11 changes: 10 additions & 1 deletion concepts/lists/links.json
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
[]
[
{
"url": "https://hackage.haskell.org/package/base/docs/Data-List.html",
"description": "Haskell Prelude: Data.List"
},
{
"url": "https://www.haskelltutorials.com/guides/haskell-lists-ultimate-guide.html",
"description": "Haskell Lists: The Ultimate Guide"
}
]
17 changes: 17 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@
"basics"
]
},
{
"slug": "language-list",
"name": "Language List",
"uuid": "2770e2c0-883d-4336-be94-3b4ae7529ac3",
"prerequisites": [
"basics"
],
"status": "beta",
"concepts": [
"lists"
]
},
{
"slug": "temperature",
"name": "Temperature",
Expand Down Expand Up @@ -1399,6 +1411,11 @@
"slug": "booleans",
"name": "Booleans"
},
{
"uuid": "ef484ab5-d4d2-4d7c-b4b5-abb5920f916a",
"slug": "lists",
"name": "Lists"
},
{
"uuid": "ca21a553-6fc1-49be-8f47-730f97330862",
"slug": "pattern-matching-literals",
Expand Down
38 changes: 33 additions & 5 deletions exercises/concept/language-list/.docs/hints.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,36 @@
# General
# Hints

- Basic numbers operators are described in the Haskell [GHC.Num module documentation](https://hackage.haskell.org/package/base-4.16.0.0/docs/GHC-Num.html). But you might prefer a more easily digestable [basic introduction.](https://www.tutorialspoint.com/haskell/haskell_basic_operators.htm)
## General

# Modules and Indentation
- Use the built-in [(linked) list type][list].

- [Declaring modules](https://learnyouahaskell.github.io/modules#making-our-own-modules)
- [Indentation rules](https://en.wikibooks.org/wiki/Haskell/Indentation)
## 1. Define a function to return an empty language list

- The function needs to return `[]`.

## 2. Define a function to add a language to the list

- An element can be prepended to a list using `:`.

## 3. Define a function to remove a language from the list

- Haskell [provides a function][tail] to return a list with the first item removed.

## 4. Define a function to return the first item in the list

- HaskellHaskell [provides a function][head] to get the first item from a list.

## 5. Define a function to return how many languages are in the list

- Haskell [provides a function][length] to count the length of a list.

## 6. Define a function to determine if the list includes a functional language

- Your function should return a boolean value indicating whether `"Haskell"` is a member of the list.
Haskell [provides a function][elem] to test list membership.

[list]: https://hackage.haskell.org/package/base/docs/Data-List.html
[head]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:head
[tail]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:tail
[elem]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:elem
[length]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:length
26 changes: 13 additions & 13 deletions exercises/concept/language-list/.docs/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,32 @@ There are several common Prelude functions for lists:
- [`head`][head] returns the _head_ of a list -- the _first_ item in a list.
- [`tail`][tail] returns the _tail_ of the list -- the list _minus_ the _first_ item.
- [`length`][length] returns the number items in the list.
- [`elem`][in] returns a boolean value indicating whether the item is an element in the list.
- [`elem`][elem] returns a boolean value indicating whether the item is an element in the list.

There is also the [`List` module][list].
There is also the [`Data.List` module][list].

Lists can only contain one data type.
## List types

In Haskell, lists are **homogenous**.
This means that all elements of a list have the same type.
When you try to put elements of different types into the same list, you will get a type error.

```haskell
list = [1, "string"]
-- Error: No instance for (Num String) arising from the literal ‘1’
```

## Type annotation

The type annotation of a list is `[a]` where a is the type which the lists holds, for example `String` or `Int`.

``` haskell
a :: [Int]
a = [1, 2, 3]
```

[enum]: https://hexdocs.pm/elixir/Enum.html
[enum-protocol]: https://hexdocs.pm/elixir/Enumerable.html
[hd]: https://hexdocs.pm/elixir/Kernel.html#hd/1
[in]: https://hexdocs.pm/elixir/Kernel.html#in/2
[length]: https://hexdocs.pm/elixir/Kernel.html#length/1
[list]: https://hexdocs.pm/elixir/List.html
[stream]: https://hexdocs.pm/elixir/Stream.html
[tl]: https://hexdocs.pm/elixir/Kernel.html#tl/1
[prelude]: https://hackage.haskell.org/package/base/docs/Prelude.html
[list]: https://hackage.haskell.org/package/base/docs/Data-List.html
[head]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:head
[tail]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:tail
[elem]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:elem
[length]: https://hackage.haskell.org/package/base/docs/Prelude.html#v:length
[linked-list-wiki]: https://en.wikipedia.org/wiki/Linked_list
2 changes: 1 addition & 1 deletion exercises/concept/language-list/.meta/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
- know of the existence of the `list` type;
- know of the idea of `list` design;
- know some basic patterns / functions
- like `[]`, `[_:_]`, `head`, `tail`, `length/1`, `elem`
- like `[]`, `[_:_]`, `head`, `tail`, `length`, `elem`
- `string-literals`
- not a standalone concept, captured in `basics`
- know how to write out a string with double quotes
Expand Down

0 comments on commit be6635e

Please sign in to comment.