From be6635e577172613e042ff1936ea366dcf6e343c Mon Sep 17 00:00:00 2001 From: meatball Date: Mon, 30 Sep 2024 20:27:54 +0200 Subject: [PATCH] Add lists concept --- concepts/lists/about.md | 26 ++++++------- concepts/lists/introduction.md | 28 +++++++------- concepts/lists/links.json | 11 +++++- config.json | 17 +++++++++ .../concept/language-list/.docs/hints.md | 38 ++++++++++++++++--- .../language-list/.docs/introduction.md | 26 ++++++------- .../concept/language-list/.meta/design.md | 2 +- 7 files changed, 101 insertions(+), 47 deletions(-) diff --git a/concepts/lists/about.md b/concepts/lists/about.md index bf76c86b8..563bfce7a 100644 --- a/concepts/lists/about.md +++ b/concepts/lists/about.md @@ -56,19 +56,21 @@ 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 @@ -76,12 +78,10 @@ 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 diff --git a/concepts/lists/introduction.md b/concepts/lists/introduction.md index d5193935d..563bfce7a 100644 --- a/concepts/lists/introduction.md +++ b/concepts/lists/introduction.md @@ -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. @@ -56,19 +56,21 @@ 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 @@ -76,12 +78,10 @@ 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 diff --git a/concepts/lists/links.json b/concepts/lists/links.json index 0637a088a..b01d42115 100644 --- a/concepts/lists/links.json +++ b/concepts/lists/links.json @@ -1 +1,10 @@ -[] \ No newline at end of file +[ + { + "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" + } +] \ No newline at end of file diff --git a/config.json b/config.json index 145ade90f..6605b37d2 100644 --- a/config.json +++ b/config.json @@ -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", @@ -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", diff --git a/exercises/concept/language-list/.docs/hints.md b/exercises/concept/language-list/.docs/hints.md index 3e2c107b8..de70d1ee0 100644 --- a/exercises/concept/language-list/.docs/hints.md +++ b/exercises/concept/language-list/.docs/hints.md @@ -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 diff --git a/exercises/concept/language-list/.docs/introduction.md b/exercises/concept/language-list/.docs/introduction.md index bf76c86b8..563bfce7a 100644 --- a/exercises/concept/language-list/.docs/introduction.md +++ b/exercises/concept/language-list/.docs/introduction.md @@ -56,19 +56,21 @@ 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 @@ -76,12 +78,10 @@ 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 diff --git a/exercises/concept/language-list/.meta/design.md b/exercises/concept/language-list/.meta/design.md index 5bbe978d3..b9fe14c8e 100644 --- a/exercises/concept/language-list/.meta/design.md +++ b/exercises/concept/language-list/.meta/design.md @@ -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