Lists are built-in to the Elixir language. They are considered a basic type, denoted by square brackets. Lists may be empty or hold any number of items of any type. For example:
empty_list = []
one_item_list = [1]
two_item_list = [1, 2]
multiple_type_list = [1, :pi, 3.14, "four"]
Elixir implements lists as a linked list, where each node stores two values: the first item and another list with all the remaining items. The first item in the list is referred to as the head and the remaining list of items is called the tail. We can use this notation in code:
# [1] represented in [head | tail] notation
[1 | []]
# [1, 2, 3] represented in [head | tail] notation
[1 | [2 | [3 | []]]]
We can use [head | tail]
notation to prepend elements to a list:
# Suppose
list = [2, 1]
[3, 2, 1] == [3 | list]
# => true
There are several functions in the Kernel
module for working with lists, as well as the whole List
module.
# Check if 1 is a member of the list
1 in [1, 2, 3, 4]
# => true
In this exercise you need to implement some functions to manipulate a list of programming languages.
Define the new/0
function that takes no arguments and returns an empty list.
LanguageList.new()
# => []
Define the add/2
function that takes 2 arguments (a language list and a string literal of a language). It should return the resulting list with the new language prepended to the given list.
language_list = LanguageList.new()
# => []
language_list = LanguageList.add(language_list, "Clojure")
# => ["Clojure"]
language_list = LanguageList.add(language_list, "Haskell")
# => ["Haskell", "Clojure"]
Define the remove/1
function that takes 1 argument (a language list). It should return the list without the first item. Assume the list will always have at least one item.
language_list = LanguageList.new()
# => []
language_list = LanguageList.add(language_list, "Clojure")
# => ["Clojure"]
language_list = LanguageList.add(language_list, "Haskell")
# => ["Haskell", "Clojure"]
language_list = LanguageList.remove(language_list)
# => ["Clojure"]
Define the first/1
function that takes 1 argument (a language list). It should return the first language in the list. Assume the list will always have at least one item.
language_list = LanguageList.new()
# => []
language_list = LanguageList.add(language_list, "Elm")
# => ["Elm"]
language_list = LanguageList.add(language_list, "Prolog")
# => ["Prolog", "Elm"]
LanguageList.first(language_list)
# => "Prolog"
Define the count/1
function that takes 1 argument (a language list). It should return the number of languages in the list.
language_list = LanguageList.new()
# => []
language_list = LanguageList.add(language_list, "Elm")
# => ["Elm"]
language_list = LanguageList.add(language_list, "Prolog")
# => ["Prolog", "Elm"]
LanguageList.count(language_list)
# => 2
Define the functional_list?/1
function which takes 1 argument (a language list). It should return a boolean value. It should return true if "Elixir" is one of the languages in the list.
language_list = LanguageList.new()
# => []
language_list = LanguageList.add(language_list, "Elixir")
# => ["Elixir"]
LanguageList.functional_list?(language_list)
# => true
Sign up to Exercism to learn and master Elixir with 57 concepts, 159 exercises, and real human mentoring, all for free.