Skip to content

Commit 8f8817f

Browse files
committed
Add list concept
1 parent 594515b commit 8f8817f

File tree

16 files changed

+550
-0
lines changed

16 files changed

+550
-0
lines changed

concepts/lists/.meta/config.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"blurb": "Lists are a basic data type in Haskell for holding a collection of values. A list can hold values of different types. Lists are immutable, meaning they cannot be modified. Lists in Haskell are implemented as linked lists. Therefore, accessing an element in a list takes linear time depending on the length of the list.",
3+
"authors": [
4+
"meatball133"
5+
]
6+
}

concepts/lists/about.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# About
2+
3+
[Lists][list] are a basic data type in Haskell for holding a collection of values.
4+
Lists are _immutable_, meaning they cannot be modified.
5+
Any operation that changes a list returns a new list.
6+
There are several methods in the prelude which allows you to work with Lists.
7+
8+
Lists in Haskell are implemented as [linked lists][linked-list-wiki], and not as arrays of contiguous memory location.
9+
Therefore, accessing an element in a list takes linear time depending on the length of the list.
10+
11+
Lists can be written in literal form, head-tail notation, (which uses the `cons` operator `:`), or a combination of both:
12+
13+
```haskell
14+
-- Literal Form
15+
[]
16+
[1]
17+
[1, 2, 3]
18+
19+
-- Head-tail Notation
20+
[]
21+
-- same as [1]
22+
1 : []
23+
-- same as [1, 2, 3]
24+
1 : (2 : (3 : []))
25+
26+
-- Mixed
27+
-- same as [1, 2, 3]
28+
1 : [2, 3]
29+
```
30+
31+
Head-tail notation can be used to append items to a list.
32+
33+
```haskell
34+
list = [2, 1]
35+
36+
[3, 2, 1] == 3 : list
37+
-- -> True
38+
```
39+
40+
Appending elements to a list during iteration is considered an anti-pattern.
41+
Appending an element requires walking through the entire list and adding the element at the end, therefore, appending a new element in each iteration would require walking through the entire list in each iteration.
42+
43+
We can achieve the same result by prepending an element to the reversed list, and then reversing the result. Prepending is a fast operation and requires constant time.
44+
45+
```haskell
46+
-- Appending to the end of a list (potentially slow)
47+
[1, 2, 3] ++ [4] ++ [5] ++ [6]
48+
49+
-- Prepend to the start of a list (faster, due to the nature of linked lists)
50+
6 : (5 : (4 : [3, 2, 1]))
51+
-- then reverse!
52+
```
53+
54+
There are several common Prelude functions for lists:
55+
56+
- [`head`][head] returns the _head_ of a list -- the _first_ item in a list.
57+
- [`tail`][tail] returns the _tail_ of the list -- the list _minus_ the _first_ item.
58+
- [`length`][length] returns the number items in the list.
59+
- [`elem`][in] returns a boolean value indicating whether the item is an element in the list.
60+
61+
There is also the [`List` module][list].
62+
63+
Lists can only contain one data type.
64+
65+
```haskell
66+
list = [1, "string"]
67+
-- Error: No instance for (Num String) arising from the literal ‘1’
68+
```
69+
70+
## Type annotation
71+
72+
The type annotation of a list is `[a]` where a is the type which the lists holds, for example `String` or `Int`.
73+
74+
``` haskell
75+
a :: [Int]
76+
a = [1, 2, 3]
77+
```
78+
79+
[enum]: https://hexdocs.pm/elixir/Enum.html
80+
[enum-protocol]: https://hexdocs.pm/elixir/Enumerable.html
81+
[hd]: https://hexdocs.pm/elixir/Kernel.html#hd/1
82+
[in]: https://hexdocs.pm/elixir/Kernel.html#in/2
83+
[length]: https://hexdocs.pm/elixir/Kernel.html#length/1
84+
[list]: https://hexdocs.pm/elixir/List.html
85+
[stream]: https://hexdocs.pm/elixir/Stream.html
86+
[tl]: https://hexdocs.pm/elixir/Kernel.html#tl/1
87+
[linked-list-wiki]: https://en.wikipedia.org/wiki/Linked_list

concepts/lists/introduction.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# About
2+
3+
[Lists][list] are a basic data type in Haskell for holding a collection of values.
4+
Lists are _immutable_, meaning they cannot be modified.
5+
Any operation that changes a list returns a new list.
6+
There are several methods in the prelude which allows you to work with Lists.
7+
8+
Lists in Haskell are implemented as [linked lists][linked-list-wiki], and not as arrays of contiguous memory location.
9+
Therefore, accessing an element in a list takes linear time depending on the length of the list.
10+
11+
Lists can be written in literal form, head-tail notation, (which uses the `cons` operator `:`), or a combination of both:
12+
13+
```haskell
14+
-- Literal Form
15+
[]
16+
[1]
17+
[1, 2, 3]
18+
19+
-- Head-tail Notation
20+
[]
21+
-- same as [1]
22+
1 : []
23+
-- same as [1, 2, 3]
24+
1 : (2 : (3 : []))
25+
26+
-- Mixed
27+
-- same as [1, 2, 3]
28+
1 : [2, 3]
29+
```
30+
31+
Head-tail notation can be used to append items to a list.
32+
33+
```haskell
34+
list = [2, 1]
35+
36+
[3, 2, 1] == 3 : list
37+
-- true
38+
```
39+
40+
Appending elements to a list during iteration is considered an anti-pattern.
41+
Appending an element requires walking through the entire list and adding the element at the end, therefore, appending a new element in each iteration would require walking through the entire list in each iteration.
42+
43+
We can achieve the same result by prepending an element to the reversed list, and then reversing the result. Prepending is a fast operation and requires constant time.
44+
45+
```haskell
46+
-- Appending to the end of a list (potentially slow)
47+
[1, 2, 3] ++ [4] ++ [5] ++ [6]
48+
49+
-- Prepend to the start of a list (faster, due to the nature of linked lists)
50+
6 : (5 : (4 : [3, 2, 1]))
51+
-- then reverse!
52+
```
53+
54+
There are several common Prelude functions for lists:
55+
56+
- [`head`][head] returns the _head_ of a list -- the _first_ item in a list.
57+
- [`tail`][tail] returns the _tail_ of the list -- the list _minus_ the _first_ item.
58+
- [`length`][length] returns the number items in the list.
59+
- [`elem`][in] returns a boolean value indicating whether the item is an element in the list.
60+
61+
There is also the [`List` module][list].
62+
63+
Lists can only contain one data type.
64+
65+
```haskell
66+
list = [1, "string"]
67+
-- Error: No instance for (Num String) arising from the literal ‘1’
68+
```
69+
70+
## Type annotation
71+
72+
The type annotation of a list is `[a]` where a is the type which the lists holds, for example `String` or `Int`.
73+
74+
``` haskell
75+
a :: [Int]
76+
a = [1, 2, 3]
77+
```
78+
79+
[enum]: https://hexdocs.pm/elixir/Enum.html
80+
[enum-protocol]: https://hexdocs.pm/elixir/Enumerable.html
81+
[hd]: https://hexdocs.pm/elixir/Kernel.html#hd/1
82+
[in]: https://hexdocs.pm/elixir/Kernel.html#in/2
83+
[length]: https://hexdocs.pm/elixir/Kernel.html#length/1
84+
[list]: https://hexdocs.pm/elixir/List.html
85+
[stream]: https://hexdocs.pm/elixir/Stream.html
86+
[tl]: https://hexdocs.pm/elixir/Kernel.html#tl/1
87+
[linked-list-wiki]: https://en.wikipedia.org/wiki/Linked_list

concepts/lists/links.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# General
2+
3+
- 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)
4+
5+
# Modules and Indentation
6+
7+
- [Declaring modules](https://learnyouahaskell.github.io/modules#making-our-own-modules)
8+
- [Indentation rules](https://en.wikibooks.org/wiki/Haskell/Indentation)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Instructions
2+
3+
In this exercise you need to implement some functions to manipulate a list of programming languages.
4+
5+
## 1. Define a function to return an empty language list
6+
7+
Define the `new` function that takes no arguments and returns an empty list.
8+
9+
```haskell
10+
new
11+
-- -> []
12+
```
13+
14+
## 2. Define a function to add a language to the list
15+
16+
Define the `add/2` function that takes 2 arguments (a _language list_ and a string literal of a _language_).
17+
It should return the resulting list with the new language prepended to the given list.
18+
19+
```haskell
20+
add new "Clojure"
21+
-- -> ["Clojure"]
22+
add ["Clojure"] "Haskell"
23+
-- -> ["Haskell", "Clojure"]
24+
```
25+
26+
## 3. Define a function to remove a language from the list
27+
28+
Define the `remove` function that takes 1 argument (a _language list_).
29+
It should return the list without the first item. Assume the list will always have at least one item.
30+
31+
```haskell
32+
remove ["Haskell", "Clojure", "Erlang"]
33+
-- -> ["Clojure", "Erlang"]
34+
```
35+
36+
## 4. Define a function to return the first item in the list
37+
38+
Define the `first` function that takes 1 argument (a _language list_).
39+
It should return the first language in the list.
40+
Assume the list will always have at least one item.
41+
42+
```haskell
43+
first ["Elixir", "Haskell", "Clojure", "Prolog"]
44+
-- -> "Elixir"
45+
```
46+
47+
## 5. Define a function to return how many languages are in the list
48+
49+
Define the `count` function that takes 1 argument (a _language list_).
50+
It should return the number of languages in the list.
51+
52+
```haskell
53+
count ["Prolog", "Elm"]
54+
-- -> 2
55+
```
56+
57+
## 6. Define a function to determine if the list includes a functional language
58+
59+
Define the `isFunctionalList` function which takes 1 argument (a _language list_).
60+
It should return a boolean value.
61+
It should return `True` if _"Haskell"_ is one of the languages in the list.
62+
63+
```haskell
64+
isFunctionalList ["Haskell"]
65+
-- -> True
66+
```
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# About
2+
3+
[Lists][list] are a basic data type in Haskell for holding a collection of values.
4+
Lists are _immutable_, meaning they cannot be modified.
5+
Any operation that changes a list returns a new list.
6+
There are several methods in the prelude which allows you to work with Lists.
7+
8+
Lists in Haskell are implemented as [linked lists][linked-list-wiki], and not as arrays of contiguous memory location.
9+
Therefore, accessing an element in a list takes linear time depending on the length of the list.
10+
11+
Lists can be written in literal form, head-tail notation, (which uses the `cons` operator `:`), or a combination of both:
12+
13+
```haskell
14+
-- Literal Form
15+
[]
16+
[1]
17+
[1, 2, 3]
18+
19+
-- Head-tail Notation
20+
[]
21+
-- same as [1]
22+
1 : []
23+
-- same as [1, 2, 3]
24+
1 : (2 : (3 : []))
25+
26+
-- Mixed
27+
-- same as [1, 2, 3]
28+
1 : [2, 3]
29+
```
30+
31+
Head-tail notation can be used to append items to a list.
32+
33+
```haskell
34+
list = [2, 1]
35+
36+
[3, 2, 1] == 3 : list
37+
-- -> True
38+
```
39+
40+
Appending elements to a list during iteration is considered an anti-pattern.
41+
Appending an element requires walking through the entire list and adding the element at the end, therefore, appending a new element in each iteration would require walking through the entire list in each iteration.
42+
43+
We can achieve the same result by prepending an element to the reversed list, and then reversing the result. Prepending is a fast operation and requires constant time.
44+
45+
```haskell
46+
-- Appending to the end of a list (potentially slow)
47+
[1, 2, 3] ++ [4] ++ [5] ++ [6]
48+
49+
-- Prepend to the start of a list (faster, due to the nature of linked lists)
50+
6 : (5 : (4 : [3, 2, 1]))
51+
-- then reverse!
52+
```
53+
54+
There are several common Prelude functions for lists:
55+
56+
- [`head`][head] returns the _head_ of a list -- the _first_ item in a list.
57+
- [`tail`][tail] returns the _tail_ of the list -- the list _minus_ the _first_ item.
58+
- [`length`][length] returns the number items in the list.
59+
- [`elem`][in] returns a boolean value indicating whether the item is an element in the list.
60+
61+
There is also the [`List` module][list].
62+
63+
Lists can only contain one data type.
64+
65+
```haskell
66+
list = [1, "string"]
67+
-- Error: No instance for (Num String) arising from the literal ‘1’
68+
```
69+
70+
## Type annotation
71+
72+
The type annotation of a list is `[a]` where a is the type which the lists holds, for example `String` or `Int`.
73+
74+
``` haskell
75+
a :: [Int]
76+
a = [1, 2, 3]
77+
```
78+
79+
[enum]: https://hexdocs.pm/elixir/Enum.html
80+
[enum-protocol]: https://hexdocs.pm/elixir/Enumerable.html
81+
[hd]: https://hexdocs.pm/elixir/Kernel.html#hd/1
82+
[in]: https://hexdocs.pm/elixir/Kernel.html#in/2
83+
[length]: https://hexdocs.pm/elixir/Kernel.html#length/1
84+
[list]: https://hexdocs.pm/elixir/List.html
85+
[stream]: https://hexdocs.pm/elixir/Stream.html
86+
[tl]: https://hexdocs.pm/elixir/Kernel.html#tl/1
87+
[linked-list-wiki]: https://en.wikipedia.org/wiki/Linked_list
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Teaches the basics concept, wherein we want the students to learn how to define functions,
2+
and we found that people usually learn best when they have to write things from scratch.
3+
https://github.com/exercism/haskell/pull/1026#issuecomment-988556486
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"authors": [
3+
"meatball133"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/LanguageList.hs",
8+
"package.yaml"
9+
],
10+
"test": [
11+
"test/Tests.hs"
12+
],
13+
"exemplar": [
14+
".meta/exemplar/src/LanguageListLanguageList.hs"
15+
],
16+
"invalidator": [
17+
"stack.yaml"
18+
]
19+
},
20+
"forked_from": [
21+
"elixir/language-list"
22+
],
23+
"icon": "language-list",
24+
"blurb": "Learn about lists by keeping track of the programming languages you're currently learning on Exercism."
25+
}

0 commit comments

Comments
 (0)