List is a collection of elements of same
type. List can be of any length, empty list is denoted by [].
For
example,
countries = ["India", "China", "Sri Lanka"] primes = [2, 3, 5, 6, 11] vowels = ['a', 'e', 'i', 'o', 'u'] emptyList = []
‘countries’ represent list of countries,
‘primes’ represent list of prime numbers, ‘vowles’ represent list of vowels.
Note that all elements of list belongs to same type.
How
to define a list
By using the keyword ‘let’, you can
define a list.
Syntax
let listName = [elem1, elem2, …. elemN]
The square brackets delimit the list,
and commas separate individual elements.
Prelude> let countries = ["India", "China", "Sri Lanka"] Prelude> let primes = [2, 3, 5, 6, 11] Prelude> let vowels = ['a', 'e', 'i', 'o', 'u'] Prelude> Prelude> countries ["India","China","Sri Lanka"] Prelude> Prelude> :t countries countries :: [[Char]] Prelude> Prelude> primes [2,3,5,6,11] Prelude> Prelude> :t primes primes :: Num t => [t] Prelude> Prelude> vowels "aeiou" Prelude> Prelude> :t vowels vowels :: [Char]
Prepend
elements to a list
By using ‘:’ operator you can prepend
and element to a list. : is pronounced as cons, it is a mnemonic for "constructor".
Prelude> primes [2,3,5,6,11] Prelude> Prelude> let new_primes = 13:primes Prelude> new_primes [13,2,3,5,6,11] Prelude> primes [2,3,5,6,11] Prelude> let new_list_1 = 13:17:19:primes Prelude> new_list_1 [13,17,19,2,3,5,6,11]
Combine
two lists
By using ++ operator, you can append one
list to other.
Prelude> let even_numbers = [2, 4, 6, 8, 10] Prelude> let odd_numbers = [1, 3, 5, 7, 9] Prelude> Prelude> let evenFollowedByOdd = even_numbers ++ odd_numbers Prelude> let oddFollowedByEven = odd_numbers ++ even_numbers Prelude> Prelude> evenFollowedByOdd [2,4,6,8,10,1,3,5,7,9] Prelude> oddFollowedByEven [1,3,5,7,9,2,4,6,8,10]
Access
elements of a list
By using !! operator, you can access the
elements of a list. Elements indexed from 0.
list !! 0 returns first element in the
list
list !! 1 returns second element in the
list.
You will get an error, if you try to
access elements from outside range. 0 <= index < noOfElementsInList.
Prelude> odd_numbers [1,3,5,7,9] Prelude> Prelude> odd_numbers !! 0 1 Prelude> odd_numbers !! 1 3 Prelude> odd_numbers !! 2 5 Prelude> odd_numbers !! 3 7 Prelude> odd_numbers !! 4 9 Prelude> odd_numbers !! 5 *** Exception: Prelude.!!: index too large Prelude> odd_numbers !! -1 <interactive>:51:1: Precedence parsing error cannot mix ‘!!’ [infixl 9] and prefix `-' [infixl 6] in the same infix expression
List can contain other lists
A list can
contain other lists.
Prelude> let numbers = [[2, 3, 5, 7], [2, 4, 6], [1, 3, 5, 7, 9, 11, 13]] Prelude> Prelude> numbers !! 0 [2,3,5,7] Prelude> numbers !! 1 [2,4,6] Prelude> numbers !! 2 [1,3,5,7,9,11,13]
All the lists inside a list must be of
same type but can be different length.
Prelude> ['a', 'b']:numbers <interactive>:62:12: No instance for (Num Char) arising from a use of ‘numbers’ In the second argument of ‘(:)’, namely ‘numbers’ In the expression: ['a', 'b'] : numbers In an equation for ‘it’: it = ['a', 'b'] : numbers Prelude> Prelude> [1, 4, 9, 16]:numbers [[1,4,9,16],[2,3,5,7],[2,4,6],[1,3,5,7,9,11,13]]
head: Get first element of the list
head takes a
list and return first element of the list.
Prelude> let primes = [2, 3, 5, 7, 11] Prelude> head primes 2
tail: Return tail of list
tail takes a
list and return a list by removing first element from it.
Prelude> primes [2,3,5,7,11] Prelude> tail primes [3,5,7,11]
last: Return last element of a list
last takes a
list and return the last element of list.
Prelude> primes [2,3,5,7,11] Prelude> last primes 11
init: Return a list by excluding last element
init takes a
list and return new list by excluding the last element.
Prelude> primes [2,3,5,7,11] Prelude> Prelude> let primesLessThan10 = init primes Prelude> primesLessThan10 [2,3,5,7]
length: Get length of the list
length takes a
list and return number of elements in the list.
Prelude> primes [2,3,5,7,11] Prelude> length primes 5
null: Check for list emptiness
null takes a
list and return True if list is empty, else False.
Prelude> primes [2,3,5,7,11] Prelude> null primes False Prelude> null [] True
reverse: Reverse a list
reverse takes a
list and return new list that contains all the elements in reverse order.
Prelude> primes [2,3,5,7,11] Prelude> reverse primes [11,7,5,3,2] Prelude> primes [2,3,5,7,11]
take:
Get n elements from list
take takes a number n and a list, return
first n elements from list. if n = 0, it returns empty list, if n > length
of list, then it returns complete list. If n < 0 , then take throws an
error.
Prelude> primes [2,3,5,7,11] Prelude> take 0 primes [] Prelude> take 2 primes [2,3] Prelude> take 7 primes [2,3,5,7,11] Prelude> take -10 primes <interactive>:30:1: Non type-variable argument in the constraint: Num (Int -> [a] -> [a]) (Use FlexibleContexts to permit this) When checking that ‘it’ has the inferred type it :: forall a t. (Num t, Num (Int -> [a] -> [a]), Num ([t] -> Int -> [a] -> [a])) => Int -> [a] -> [a]
drop:
Drop elements from beginning of a list
drop takes a number n and a list, return
a new list by dropping first n elements. If n > number of elements in list,
then it drops all the elements. If n < 0, it throws error.
Prelude> primes [2,3,5,7,11] Prelude> drop 0 primes [2,3,5,7,11] Prelude> drop 3 primes [7,11] Prelude> drop 7 primes [] Prelude> drop -7 primes <interactive>:37:1: Non type-variable argument in the constraint: Num (Int -> [a] -> [a]) (Use FlexibleContexts to permit this) When checking that ‘it’ has the inferred type it :: forall a t. (Num t, Num (Int -> [a] -> [a]), Num ([t] -> Int -> [a] -> [a])) => Int -> [a] -> [a]
maximum: Get maximum element from a list
maximum takes a
list and return maximum element in the list.
minimum: Get minimum element from a list
minimum takes a
list and return minimum element in the list.
Prelude> primes [2,3,5,7,11] Prelude> maximum primes 11 Prelude> minimum primes 2
sum: Get sum of elements in the list
sum takes a
list of numbers and return sum of elements in the list.
Prelude> primes [2,3,5,7,11] Prelude> sum primes 28
product: get product of elements in the list
product takes a
list and return product of all numbers in the list.
Prelude> primes [2,3,5,7,11] Prelude> product primes 2310
elem: Search for existence of an element
elem takes an
element and list, return True if the element is in list, else False.
Prelude> primes [2,3,5,7,11] Prelude> Prelude> 2 `elem` primes True Prelude> 10 `elem` primes False
No comments:
Post a Comment