An Array
in Elm is a fast immutable collection of zero or more values of the same type.
Unlike List
which is a default-import, the Array
module must be explicity imported, using import Array
or similar.
Arrays can be defined as follows:
import Array exposing (Array)
empty : Array Char
empty = Array.empty
singleValue = Array.fromList [ 5 ] --> Array Int
repeatSingleValue = Array.repeat 2 5 --> Array.fromList [ 5, 5 ]
initialiseFromIndex = Array.initialise 3 (\index -> index * 2) --> Array.fromList [ 0, 2, 4 ]
initialisefromList = Array.fromList [ "a", "b", "c" ] --> Array.fromList [ "a", "b", "c" ]
Elements are added to an Array using Array.push
:
twoToFour = Array.fromList [ 2, 3, 4 ]
oneToFour = Array.push 1 twoToFour --> Array.fromList [ 1, 2, 3, 4 ]
An element in an Array is retrieved using Array.get
:
sixSeven = Array.fromList [ 6, 7 ]
Array.get 0 sixSeven --> Just 6
An element in an Array is set using Array.set
:
sixSeven = Array.fromList [ 6, 7 ]
Array.set 1 8 sixSeven --> Array.fromList [ 6, 8 ]
Arrays are manipulated by functions defined in the Array
module.
oneTwo = Array.fromList [ 1, 2 ]
Array.length oneTwo --> 2
As a magician-to-be, Elyse needs to practice some basics. She has a deck of cards that she wants to manipulate.
To make things a bit easier she only uses the cards 1 to 10.
Implement the getCard
function that returns the card at position index
from the given deck. If the index is not in the deck, return Nothing.
getCard 2, ( Array.fromList [ 1, 2, 4, 1 ] )
--> Just 4
getCard 6, ( Array.fromList [ 1, 2, 4, 1 ] )
--> Nothing
Implement the setCard
function that returns a deck with the card at position index
changed to the new card provided.
If the index
is not in the deck, return the original deck.
setCard 2, 6, ( Array.fromList [ 1, 2, 4, 1 ] )
--> Array.fromList [ 1, 2, 6, 1 ]
setCard 6, 6, ( Array.fromList [ 1, 2, 4, 1 ] )
--> Array.fromList [ 1, 2, 4, 1 ]
Implement the addCard
function that returns a deck with the new card added.
addCard 8 ( Array.fromList [ 5, 9, 7, 1 ] )
--> Array.fromList [ 5, 9, 7, 1, 8 ]
Implement the removeCard
function that returns a deck with the card at position index
removed.
If the given index
is not a valid index in the deck, return the original deck.
removeCard 2 ( Array.fromList [ 3, 2, 6, 4, 8 ] )
--> Array.fromList [ 3, 2, 4, 8 ]
removeCard 2 ( Array.fromList [ 3, 2 ] )
--> Array.fromList [ 3, 2 ]
Implement the evenCardCount
function that counts the number of even cards in the deck.
evenCardCount (Array.fromList [ 3, 8, 4, 5, 1, 6, 10 ] )
--> 4
Sign up to Exercism to learn and master Elm with 22 concepts, 93 exercises, and real human mentoring, all for free.