SlideShare a Scribd company logo
1 of 15
Download to read offline
Introduction to FP with Haskell
Sambaiah Kilaru
Sambaiah Kilaru Introduction to FP with Haskell
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
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
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
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
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
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
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
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
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
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
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
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
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
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

More Related Content

What's hot

Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesRanel Padon
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm KristinaBorooah
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear listsToniyaP1
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structureeShikshak
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISPNilt1234
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
Introduction to Programming in LISP
Introduction to Programming in LISPIntroduction to Programming in LISP
Introduction to Programming in LISPKnoldus Inc.
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10Bianca Teşilă
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISPDevnology
 
Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R LanguageGaurang Dobariya
 

What's hot (20)

Python Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and DictionariesPython Programming - V. Sequences (List and Tuples) and Dictionaries
Python Programming - V. Sequences (List and Tuples) and Dictionaries
 
Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm Arrays in Data Structure and Algorithm
Arrays in Data Structure and Algorithm
 
LISP: Data types in lisp
LISP: Data types in lispLISP: Data types in lisp
LISP: Data types in lisp
 
LISP: Introduction to lisp
LISP: Introduction to lispLISP: Introduction to lisp
LISP: Introduction to lisp
 
Data structures: linear lists
Data structures: linear listsData structures: linear lists
Data structures: linear lists
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Pa1 session 2
Pa1 session 2 Pa1 session 2
Pa1 session 2
 
LISP: Input And Output
LISP: Input And OutputLISP: Input And Output
LISP: Input And Output
 
INTRODUCTION TO LISP
INTRODUCTION TO LISPINTRODUCTION TO LISP
INTRODUCTION TO LISP
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Regular Expressions
Regular ExpressionsRegular Expressions
Regular Expressions
 
Lisp
LispLisp
Lisp
 
Lisp
LispLisp
Lisp
 
L7
L7L7
L7
 
Introduction to Programming in LISP
Introduction to Programming in LISPIntroduction to Programming in LISP
Introduction to Programming in LISP
 
Data structures
Data structuresData structures
Data structures
 
Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4Anton Kasyanov, Introduction to Python, Lecture4
Anton Kasyanov, Introduction to Python, Lecture4
 
Data structures and algorithms lab10
Data structures and algorithms lab10Data structures and algorithms lab10
Data structures and algorithms lab10
 
Learn a language : LISP
Learn a language : LISPLearn a language : LISP
Learn a language : LISP
 
Introduction To R Language
Introduction To R LanguageIntroduction To R Language
Introduction To R Language
 

Similar to haskell_fp1

Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1Hang Zhao
 
Introduction to Functional Languages
Introduction to Functional LanguagesIntroduction to Functional Languages
Introduction to Functional Languagessuthi
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)Bikram Thapa
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsVasil Remeniuk
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsKirill Kozlov
 
Functional programming using haskell notes iitk
Functional programming using haskell notes iitkFunctional programming using haskell notes iitk
Functional programming using haskell notes iitkbenevolent001
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Philip Schwarz
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Mattersromanandreg
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture TwoAngelo Corsaro
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLSimone Di Maulo
 
1.4 the basic language of functions
1.4 the basic language of functions1.4 the basic language of functions
1.4 the basic language of functionsmath123c
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in ScalaKnoldus Inc.
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closuresKnoldus Inc.
 
1 functions
1 functions1 functions
1 functionsTzenma
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Bryan O'Sullivan
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskellgoncharenko
 

Similar to haskell_fp1 (20)

Fp in scala part 1
Fp in scala part 1Fp in scala part 1
Fp in scala part 1
 
Introduction to Functional Languages
Introduction to Functional LanguagesIntroduction to Functional Languages
Introduction to Functional Languages
 
Functional programming seminar (haskell)
Functional programming seminar (haskell)Functional programming seminar (haskell)
Functional programming seminar (haskell)
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
Functors
FunctorsFunctors
Functors
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
 
Functional programming using haskell notes iitk
Functional programming using haskell notes iitkFunctional programming using haskell notes iitk
Functional programming using haskell notes iitk
 
Frp2016 3
Frp2016 3Frp2016 3
Frp2016 3
 
Sequence and Traverse - Part 3
Sequence and Traverse - Part 3Sequence and Traverse - Part 3
Sequence and Traverse - Part 3
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Programming in Scala - Lecture Two
Programming in Scala - Lecture TwoProgramming in Scala - Lecture Two
Programming in Scala - Lecture Two
 
pyton Notes6
 pyton Notes6 pyton Notes6
pyton Notes6
 
On fuctional programming, high order functions, ML
On fuctional programming, high order functions, MLOn fuctional programming, high order functions, ML
On fuctional programming, high order functions, ML
 
1.4 the basic language of functions
1.4 the basic language of functions1.4 the basic language of functions
1.4 the basic language of functions
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
 
Functions & Closures in Scala
Functions & Closures in ScalaFunctions & Closures in Scala
Functions & Closures in Scala
 
Functions & closures
Functions & closuresFunctions & closures
Functions & closures
 
1 functions
1 functions1 functions
1 functions
 
Real World Haskell: Lecture 6
Real World Haskell: Lecture 6Real World Haskell: Lecture 6
Real World Haskell: Lecture 6
 
Functional Programming by Examples using Haskell
Functional Programming by Examples using HaskellFunctional Programming by Examples using Haskell
Functional Programming by Examples using Haskell
 

haskell_fp1

  • 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