You are on page 1of 4

Experiment No.

Aim: Introduction to Haskell Inbuilt Functions


Theory: Haskell inbuilt functions can be classified into following categories:
(a) Functions for Arithmetic Operations

This function accepts a number that has a defined successor and


1 succ returns that successor.
Eg: succ 9 Ans. 10
This function accepts a number that has a defined predecessor
2 pred and returns that predecessor.
Eg: pred 9 Ans. 8
This function accepts two numbers and returns the one that's
3 max greater.
Eg: max 5 4 Ans. 5
This function accepts two numbers and returns the one that's
4 min lesser.
Eg: min 23 12 Ans. 12
This function accepts a number and returns its square root.
5 sqrt
Eg: sqrt 9 Ans. 3.0
This function accepts two numbers and returns the Greatest
6 gcd Common Divisor of two numbers.
Eg: gcd 21 14 Ans. 7
This function accepts two numbers and returns the Least
7 lcm Common Multiple of two numbers.
Eg: lcm 12 8 Ans. 24
This function accepts a number and checks whether the number
8 even is even or not.
Eg: even 5 Ans. False
This function accepts a number and checks whether the number
9 odd is odd or not.
Eg: odd 5 Ans. True
This function accepts a list and simply adds up all the contents
10 sum of a list of numbers.
Eg: sum [5,2,1,6] Ans. 14
This function accepts a list and simply multiplies all the contents
11 product of a list of numbers.
Eg: product [6,2,1,2] Ans. 24

(b) Functions for Tuples

This function accepts a 2-tuple or a pair and returns the first


1 fst element of that tuple.
Eg: fst (2, “apple”) Ans. 2
This function accepts a 2-tuple or a pair and returns the second
2 snd element of that tuple.
Eg: snd (2, “apple”) Ans. “apple”
This function accepts two lists of (possibly different) things and
turns them into a list of 2-tuples. The first element of each of these
tuples comes from the first list, and the second element comes
3 zip
from the second list.
Eg: zip [1..5] Ans.
[“one”,“two”,“three”] [(1,“one”),(2,“two”),(3,“three”)]
This function accepts a list of tuples and separates them into two
different lists. It is the reverse process of zip.
4 unzip
Eg: unzip [(1, 'A'), (6, 'B'), Ans. ([1, 6, 3, 23], "ABCD")
(3, 'C'), (23, 'D')]

(c) Functions for Lists

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]

(d) Concept of Ranges

• Haskell allows a list of numbers to be generated by simply providing the range.


• What if the requirement is to generate a list of numbers between the range 1 to
20.
• One way is to manually type all the numbers like
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]
• The other simpler method supported by Haskell is to directly state the range like
[1..20]. This command will produce the same result as above.
• A few more example of ranges are as follows:
To generate the even numbers between 1 and 20, the command would be
[2,4..20]. (i.e. [firstnumber, secondnumber..lastnumber])

Examples:-

1. Execute the following functions and observe the result:


(a) null []
(b) take 5 [1,2]
(c) take 0 [6,6,6]
(d) drop 0 [1,2,3,4]
(e) drop 100 [1,2,3,4]
(f) zip [5,3,2,6,2,7,2,5,4,6,6] [“im”, “a”, “student”]
(g) zip [1..] [“apple”, “orange”, “cherry”, “mango”]
(h) 1:2:3:4:[]
(i) 1:[2,3]
(j) [‘a’..’z’]
(k) [‘0’..’9’]
(l) [0..9]

2. Using the concept of Ranges, solve the following:


(a) Generate the multiples of 3 between the range 1 to 20.
(b) Obtain a reverse of a list without using the reverse function between the range 1 to 20.
(c) Determine the first 24 multiple of 13 using arithmetic expressions as elements of the
list.

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.

You might also like