Tracks
/
Elixir
Elixir
/
Exercises
/
Language List
Language List

Language List

Learning Exercise

Introduction

Lists

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

Instructions

In this exercise you need to implement some functions to manipulate a list of programming languages.

1. Define a function to return an empty language list

Define the new/0 function that takes no arguments and returns an empty list.

LanguageList.new()
# => []

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

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"]

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

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"]

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

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"

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

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

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

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
Edit via GitHub The link opens in a new window or tab
Elixir Exercism

Ready to start Language List?

Sign up to Exercism to learn and master Elixir with 57 concepts, 159 exercises, and real human mentoring, all for free.