1. Introduction to FP with Haskell
Sambaiah Kilaru
Sambaiah Kilaru Introduction to FP with Haskell
2. Data types: Int, Char, Bool and Function
Type is defined with ::, A fucntion is also defined as f :: a →
b, Lower case for type and upper case for instance of type
List contains elements of same type [1,2,3,4] or it can be
written as 1:(2:(:3(4: []))), : read as cons constructor
List comprehension is defined as [y | y ← [1,2,3] ]
++ is list append operation, | are guards
Sambaiah Kilaru Introduction to FP with Haskell
3. Let us find out lenght of a list
len1 :: [a] → Int
len1 [] = 0
len1 (x:xs) = 1 + len1 xs
Functions can be composed, double :: Int → Int double x = 2
* x, quad = double . double
We know very well function composition, f and g are
functions, f circle g
Sambaiah Kilaru Introduction to FP with Haskell
4. Lazyness is virtue, work is done on time, some times not doing
work helps
f :: [a] → Int f x = 3 We saw this example
Curry - Curry is not for removing braces, we get functions
from multiple arguments can be converted as multiple
functions of single arguments
add1 :: Int → Int → Int is curried function, currying gives
multiple functions which are useful to reason and function
composition is easy
Sambaiah Kilaru Introduction to FP with Haskell
5. Problem: Given a list of lists, provide total number of
elements in all lists
what is the type of the function f :: [[a]] → Int
We know how to find length of list, we need to apply for all
elements, we get list of integers. We need to sum to get all
elements
Applying a function to each element of a list - A standard use
case we can abstract and give a name
Given a list, reducing to single element is also pretty much use
case, we can abstract and provide a name
Sambaiah Kilaru Introduction to FP with Haskell
6. Square every element of a list
squares1 :: [Int] → [Int] squares1 xs = [ x * x | x ← xs]
A recursive definition is
squares1 [] = []
squares1 (x:xs) = x * x : squares1 xs
How does Recursion works
Sambaiah Kilaru Introduction to FP with Haskell
7. Find the odd elements of list
odds :: [Int] → [Int] odds xs = [ x | x ← xs, odd x]
The above example is list comprehension
Recursion odd [] = []
odds (x:xs) | odd x = x : odds xs
| otherwise = odds xs
Sambaiah Kilaru Introduction to FP with Haskell
8. qsort1 :: [Int] ← [Int]
qsort1 [] = []
qsort1 (x:xs) = qsort1 lower ++ [x] ++ qsort1 bigger
where
lower = [y | y ← xs, y ≤ x]
bigger = [y | y ← xs, y > x]
Concurency: You can compute lower and bigger independently
as they are not related to other
Sambaiah Kilaru Introduction to FP with Haskell
9. We covered data types, functions, composition of functions
Recursion
List Comprehension
If we have a function, we have use case of applying to elemtns
of a list, we have recursion in hand to do it
Sambaiah Kilaru Introduction to FP with Haskell
10. Let f :: a → b be a function, a general case is apply on list of
a’s the function f
Some examples of applying to list given a function
g :: [a] → [b] define g which takes list and applies on each a,
the function f
This is a very general case, we can give a name as
applyingtolistfunction (not a good choice
g though dependent on f, not very much related,
applyingtolistfunction is a function, reason about its type
Sambaiah Kilaru Introduction to FP with Haskell
11. Given a function, we would like to apply to each element of a
list but now we don’t get list
add applied on list of integers gives integer
What is the type of this function
Sambaiah Kilaru Introduction to FP with Haskell
12. We have functions as first class objects, compose them what
we need to look if we see new functions
associativity, identity, commutativity, distributivity, zero and
idempotence
Associtite operation is important as you can remove excessive
braces
Functions which are associative run faster
Sambaiah Kilaru Introduction to FP with Haskell
13. What is programming?
primitive expressions, which represent the simplest entities the
language is concerned with
means of combination, by which compound elements are built
from simpler ones
means of abstraction, by which compound elements can be
named and manipulated as units
Sambaiah Kilaru Introduction to FP with Haskell
14. map is a type of function if you give input a function of type a
to type b and list of type a, gives list of type b, f applied to
each element of the list
Input to map: Function, list of domain of the chosen function,
output is list with type as codomain of Function
map :: a → b → [a] → [b]
If a function which given domain as DNS name, function is
crawler and gets text data, what does the map do?
How is Hadoop map in mapreduce related to the map
function we learned?
What are key, value in the FP map function?
Sambaiah Kilaru Introduction to FP with Haskell
15. Madhavan Mukund and Suresh of CMI lectures at NPTEL
Erik Meijer Lectures at Channel 9 or C9
Multiple online books are available
Introduction to Functional Programming Richard Bird and
Philip Wadler
Paper on why functional Programming matters
Sambaiah Kilaru Introduction to FP with Haskell