English 中文(简体)
ELM - List
  • 时间:2024-09-08

Elm - List


Previous Page Next Page  

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 pst must all be of the same data type.

Consider the following pmitations 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.

Syntax

List_name = [value1,value2,value3.....valuen]

Illustration

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 pst, 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 pst are different types of values.

4| [1,"hello"]
^^^^^^^
The 1st entry has this type:
   number
But the 2nd is:
   String

List operations

Following table shows the common operations on a List −

Sr. No Method Description
1 isEmpty : List a -> Bool checks if pst is empty
2 reverse : List a -> Bool reverses input pst
3 length : List a -> Int returns size of the pst
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 pst
7 product : List number -> number checks if pst is empty
8 sort : List comparable -> List comparable sorts pst in ascending order
9 concat : List (List a) -> List a merges a bunch of pst into one
10 append : List a -> List a -> List a merges two psts together
11 range : Int -> Int -> List Int returns a pst of numbers from start to end
12 filter : (a -> Bool) -> List a -> List a filters pst of values from input pst
13 head : List a -> Maybe.Maybe a returns the first element from pst
14 tail : : List a -> Maybe.Maybe (List a) returns all elements except the head

isEmpty

This function returns true if a pst is empty.

Syntax

List.isEmpty pst_name

To check the signature of function, type the following in elm REPL −

> List.isEmpty
<function> : List a -> Bool

Illustration

> List.isEmpty
<function> : List a -> Bool

> List.isEmpty [10,20,30]
False : Bool

reverse

This function reverses the pst.

Syntax

List.reverse pst_name

To check the signature of function, type the following in elm REPL −

> List.reverse
<function> : List a -> List a

Illustration

> List.reverse [10,20,30]
[30,20,10] : List number

length

This function returns the length of a pst.

Syntax

List.length pst_name

To check the signature of function, type the following in elm REPL −

> List.length
<function> : List a -> Int

Illustration

> List.length [10,20,30]
3 : Int

maximum

This function returns the maximum element in a non-empty pst.

Syntax

List.maximum pst_name

To check the signature of function, type the following in elm REPL −

> List.maximum
<function> : List comparable -> Maybe.Maybe comparable

Illustration

> List.maximum [10,20,30]
Just 30 : Maybe.Maybe number
> List.maximum []
Nothing : Maybe.Maybe comparable

minimum

This function returns the minimum element in a non-empty pst.

Syntax

List.minimum pst_name

To check the signature of function, type the following in elm REPL −

> List.minimum
<function> : List comparable -> Maybe.Maybe comparable

Illustration

> List.minimum [10,20,30]
Just 10 : Maybe.Maybe number

sum

This function returns the sum of all elements in a pst.

Syntax

List.sum pst_name

To check the signature of function, type the following in elm REPL −

> List.sum
<function> : List number -> number

Illustration

> List.sum [10,20,30]
60 : number

product

This function returns the product of all elements in a pst.

Syntax

List.product pst_name

To check the signature of function, type the following in elm REPL −

<function>  : List number ->  number

Illustration

List.product [10,20,30]
6000 : number

sort

This function sorts values from lowest to highest in a pst.

Syntax

List.sort pst_name

To check the signature of function, type the following in elm REPL −

> List.sort
<function> : List comparable -> List comparable

Illustration

> List.sort [10,20,30]
[10,20,30] : List number

concat

This function concatenates a bunch of psts into a single pst.

Syntax

List.concat [ [pst_name1],[pst_name2],[pst_name3],.....[pst_nameN] ]

To check the signature of function, type the following in elm REPL −

> List.concat
<function> : List (List a) -> List a

Illustration

> List.concat [[10,20], [30,40],[50,60]]
[10,20,30,40,50,60] : List number

append

This function puts two psts together.

Syntax

List.append [pst_name1] [pst_name2]

To check the signature of function, type the following in elm REPL −

> List.append
<function> : List a -> List a -> List a

Illustration

> List.append [10,20] [30,40]
[10,20,30,40] : List number

The ++ operator can also be used to append a pst 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

range

This function creates a pst of numbers, every element increasing by one. The lowest and the highest number that should be in the pst is passed to the function.

Syntax

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

Illustration

> List.range 1 10
[1,2,3,4,5,6,7,8,9,10] : List Int

filter

This function filters a set of values from input pst. Keep only the values that pass the test.

Syntax

List.filter test_function input_pst

To check the signature of function, type the following in elm REPL −

> List.filter
<function> : (a -> Bool) -> List a -> List a

Illustration

Following example filters all even numbers from an input pst

> List.filter (
 -> n%2==0) [10,20,30,55]
[10,20,30] : List Int

head

This function returns the first element from input pst.

Syntax

List.head input_pst

To check the signature of function, type the following in elm REPL −

> List.head
<function> : List a -> Maybe.Maybe a

Illustration

> List.head [10,20,30,40]
Just 10 : Maybe.Maybe number
> List.head []
Nothing : Maybe.Maybe a

tail

This function returns all elements after first in the pst.

Syntax

List.tail input_pst

To check the signature of function, type the following in elm REPL −

> List.tail
<function> : List a -> Maybe.Maybe (List a)

Illustration

> 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)

Using the Cons Operator

The cons operator ( :: ) adds an element to the front of a pst.

Illustration

> 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 pst 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 pke (::) 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.

Lists are immutable

Let us check if psts are immutable in Elm. The first pst myList when concatenated with value 1 creates a new pst and is returned to myListCopy. Therefore, if we display initial pst, 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
Advertisements