A List
in Elm is an immutable collection of zero or more values of the same type.
Lists can be defined as follows:
empty = []
singleValue = [ 5 ]
threeValues = [ "a", "b", "c" ]
The most common way to add an element to a list is through the ::
(cons) operator:
twoToFour = [ 2, 3, 4 ]
oneToFour = 1 :: twoToFour --> [ 1, 2, 3, 4 ]
Lists are manipulated by functions and operators defined in the List
module or by pattern matching
describe : List String -> String
describe list =
case list of
[] ->
"Empty list"
[ "hello" ] ->
"Singleton list with: hello"
x :: xs ->
"Non-empty list with head: " ++ x
describe [] --> "Empty list"
describe [ "hello" ] --> "Singleton list with: hello"
describe [ "hello", "world" ] --> "Non-empty list with head: hello"
In this exercise you'll be writing code to keep track of a list of programming languages you want to learn on Exercism. You have six tasks, which will all involve dealing with lists.
To keep track of the languages you want to learn, you'll need to create a new list.
Define the newList
constant that contains a new, empty list.
newList
--> []
Currently, you have a piece of paper listing the languages you want to learn: Elm, Clojure and Haskell.
Define the existingList
constant to represent this list.
existingList
--> [ "Elm", "Clojure", "Haskell" ]
As you explore Exercism and find more interesting languages, you want to add them to your list.
Implement the addLanguage
function to add a new language to the beginning of your list.
addLanguage "TypeScript" [ "JavaScript", "CoffeeScript" ]
--> [ "TypeScript", "JavaScript", "CoffeeScript" ]
Counting the languages manually is inconvenient.
Implement the countLanguages
function to count the number of languages on your list.
countLanguages [ "C#", "Racket", "Rust", "Ruby" ]
--> 4
At some point, you realize that your list is actually ordered backwards!
Implement the reverseList
function to reverse your list.
reverseList [ "Prolog", "C", "Idris", "Assembly" ]
--> [ "Assembly", "Idris", "C", "Prolog" ]
While you love all languages, Elm has a special place in your heart. As such, you're really excited about a list of languages if:
Implement the excitingList
function to check if a list of languages is exciting:
excitingList [ "Nim", "Elm" ]
--> True
Sign up to Exercism to learn and master Elm with 25 concepts, 96 exercises, and real human mentoring, all for free.