The List, Tuples and Record data structures can be used to store a collection of values.
This chapter discusses how to use List in Elm.
A List is a collection of homogeneous values. The values in a list must all be of the same data type.
Consider the following limitations while using variables to store values −
Variables are scalar in nature. In other words, at the time of declaration a variable can hold only one value. This means that to store n values in a program, n variable declarations will be needed. Hence, the use of variables is not feasible when one needs to store a larger collection of values.
Variables in a program are allocated memory in random order, thereby making it difficult to retrieve/read the values in the order of their declaration.
List_name = [value1,value2,value3.....valuen]
The following example shows how to use a List in Elm. Try this example in elm REPL −
> myList1 = [10,20,30] [10,20,30] : List number > myList2 = ["hello","world"] ["hello","world"] : List String
If we try adding values of different types into a list, the compiler will throw a type mismatch error. This is shown below.
> myList = [1,"hello"] -- TYPE MISMATCH --------------------------------------------- repl-temp-000.elm The 1st and 2nd entries in this list are different types of values. 4| [1,"hello"] ^^^^^^^ The 1st entry has this type: number But the 2nd is: String
Following table shows the common operations on a List −
Sr. No | Method | Description |
---|---|---|
1 | isEmpty : List a -> Bool | checks if list is empty |
2 | reverse : List a -> Bool | reverses input list |
3 | length : List a -> Int | returns size of the list |
4 | maximum : List comparable -> Maybe.Maybe comparable | returns maximum value |
5 | minimum : List comparable -> Maybe.Maybe comparable | returns minimum value |
6 | sum : List number -> number | returns sum of all elements in list |
7 | product : List number -> number | checks if list is empty |
8 | sort : List comparable -> List comparable | sorts list in ascending order |
9 | concat : List (List a) -> List a | merges a bunch of list into one |
10 | append : List a -> List a -> List a | merges two lists together |
11 | range : Int -> Int -> List Int | returns a list of numbers from start to end |
12 | filter : (a -> Bool) -> List a -> List a | filters list of values from input list |
13 | head : List a -> Maybe.Maybe a | returns the first element from list |
14 | tail : : List a -> Maybe.Maybe (List a) | returns all elements except the head |
This function returns true if a list is empty.
List.isEmpty list_name
To check the signature of function, type the following in elm REPL −
> List.isEmpty <function> : List a -> Bool
> List.isEmpty <function> : List a -> Bool > List.isEmpty [10,20,30] False : Bool
This function reverses the list.
List.reverse list_name
To check the signature of function, type the following in elm REPL −
> List.reverse <function> : List a -> List a
> List.reverse [10,20,30] [30,20,10] : List number
This function returns the length of a list.
List.length list_name
To check the signature of function, type the following in elm REPL −
> List.length <function> : List a -> Int
> List.length [10,20,30] 3 : Int
This function returns the maximum element in a non-empty list.
List.maximum list_name
To check the signature of function, type the following in elm REPL −
> List.maximum <function> : List comparable -> Maybe.Maybe comparable
> List.maximum [10,20,30] Just 30 : Maybe.Maybe number > List.maximum [] Nothing : Maybe.Maybe comparable
This function returns the minimum element in a non-empty list.
List.minimum list_name
To check the signature of function, type the following in elm REPL −
> List.minimum <function> : List comparable -> Maybe.Maybe comparable
> List.minimum [10,20,30] Just 10 : Maybe.Maybe number
This function returns the sum of all elements in a list.
List.sum list_name
To check the signature of function, type the following in elm REPL −
> List.sum <function> : List number -> number
> List.sum [10,20,30] 60 : number
This function returns the product of all elements in a list.
List.product list_name
To check the signature of function, type the following in elm REPL −
<function> : List number -> number
List.product [10,20,30] 6000 : number
This function sorts values from lowest to highest in a list.
List.sort list_name
To check the signature of function, type the following in elm REPL −
> List.sort <function> : List comparable -> List comparable
> List.sort [10,20,30] [10,20,30] : List number
This function concatenates a bunch of lists into a single list.
List.concat [ [list_name1],[list_name2],[list_name3],.....[list_nameN] ]
To check the signature of function, type the following in elm REPL −
> List.concat <function> : List (List a) -> List a
> List.concat [[10,20], [30,40],[50,60]] [10,20,30,40,50,60] : List number
This function puts two lists together.
List.append [list_name1] [list_name2]
To check the signature of function, type the following in elm REPL −
> List.append <function> : List a -> List a -> List a
> List.append [10,20] [30,40] [10,20,30,40] : List number
The ++ operator can also be used to append a list to another. This is shown in the example below −
> [10.1,20.2] ++ [30.3,40.4] [10.1,20.2,30.3,40.4] : List Float
This function creates a list of numbers, every element increasing by one. The lowest and the highest number that should be in the list is passed to the function.
List.range start_range end_range
To check the signature of function, type the following in elm REPL −
> List.range <function> : Int -> Int -> List Int
> List.range 1 10 [1,2,3,4,5,6,7,8,9,10] : List Int
This function filters a set of values from input list. Keep only the values that pass the test.
List.filter test_function input_list
To check the signature of function, type the following in elm REPL −
> List.filter <function> : (a -> Bool) -> List a -> List a
Following example filters all even numbers from an input list
> List.filter (\n -> n%2==0) [10,20,30,55] [10,20,30] : List Int
This function returns the first element from input list.
List.head input_list
To check the signature of function, type the following in elm REPL −
> List.head <function> : List a -> Maybe.Maybe a
> List.head [10,20,30,40] Just 10 : Maybe.Maybe number > List.head [] Nothing : Maybe.Maybe a
This function returns all elements after first in the list.
List.tail input_list
To check the signature of function, type the following in elm REPL −
> List.tail <function> : List a -> Maybe.Maybe (List a)
> List.tail [10,20,30,40,50] Just [20,30,40,50] : Maybe.Maybe (List number) > List.tail [10] Just [] : Maybe.Maybe (List number) > List.tail [] Nothing : Maybe.Maybe (List a)
The cons operator ( :: ) adds an element to the front of a list.
> 10::[20,30,40,50] [10,20,30,40,50] : List number
The new element to be added and the data-type of the values in the list must match. The compiler throws an error if the data types do not match.
> [1,2,3,4]::[5,6,7,8] -- TYPE MISMATCH --------------------------------- ------------ repl-temp-000.elm The right side of (::) is causing a type mismatch. 3| [1,2,3,4]::[5,6,7,8] ^^^^^^^^^ (::) is expecting the right side to be a: List (List number) But the right side is: List number Hint: With operators like (::) I always check the left side first. If it seems fine, I assume it is correct and check the right side. So the problem may be in how the left and right arguments interact.
Let us check if lists are immutable in Elm. The first list myList when concatenated with value 1 creates a new list and is returned to myListCopy. Therefore, if we display initial list, its values will not be changed.
> myList = [10,20,30] [10,20,30] : List number > myListCopy = 1::myList [1,10,20,30] : List number > myList [10,20,30] : List number >myList == myListCopy False : Bool