Professional Documents
Culture Documents
Experiment No. 2: (A) Functions For Arithmetic Operations
Experiment No. 2: (A) Functions For Arithmetic Operations
This function accepts a list and returns the first element of the
1 head list.
Eg: head [5,4,3,2,1] Ans. 5
This function accepts a list and returns the list without the first
2 tail element.
Eg: tail [5,4,3,2,1] Ans. [4,3,2,1]
This function accepts a list and returns the last element of the
3 last list.
Eg: last [5,4,3,2,1] Ans. 1
This function accepts a list and returns the list without the last
4 init element.
Eg: init [5,4,3,2,1] Ans. [5,4,3,2]
This function accepts a list and returns its length.
5 length
Eg: length [5,4,3,2,1] Ans. 5
This function accepts a list and checks whether the list is empty
6 null or not.
Eg: null [1,2,3] Ans. False
This function accepts a list and returns list with elements in the
7 reverse reverse order.
Eg: reverse [5,4,3,2,1] Ans. [1,2,3,4,5]
This function accepts a number and a list and extracts that many
8 take elements from the beginning of the list.
Eg: take 3 [5,4,3,2,1] Ans. [5,4,3]
This function accepts a number and a list and drops that many
9 drop elements from the beginning of the list.
Eg: drop 3 [5,4,3,2,1] Ans. [2,1]
This function accepts a list and returns the biggest element of
10 maximum the list.
Eg: maximum [1,9,4,2,3] Ans. 9
This function accepts a list and returns the smallest element of
11 minimum the list.
Eg: minimum [8,4,2,1,5,6] Ans. 1
This function accepts an element and a list and checks whether
that element is a part of that list or not.
12 elem Eg: 4 `elem` [3,4,5,6] Ans. True
or
elem 4 [3,4,5,6]
This function accepts another function and a list as input and
applies the function to every element in the list.
13 map
Eg: map (* 10) [1,2,3] Ans. [10,20,30]
This function is like map except that it takes two lists as input.
Like zip, the longer list’s extra elements are ignored.
14 zipWith
Eg: zipWith (+) [2,4..10] Ans. [3,7,11,15,19]
[1,3..10]
Examples:-
3. Evaluate the following function manually. Also, Check the result using the ghc compiler.
snd(maximum(zip [1,4,12,23] “ABCD”))
Reading Exercise:-
1. Study about the following functions:
(a) cycle
(b) repeat
(c) replicate
(d) takeWhile
(e) dropWhile
(f) notElem
2. Determine the role of take function for the cycle and repeat function.
Note: (I) fst and snd only works for 2-tuple or pairs.
(II) zip and unzip outputs a list whose length is equal to the length of the smaller of the
two lists.
(III) maximum and minimum function of the list works fine for any ordering (may not
be necessarily ascending or descending) of the input elements.