SlideShare a Scribd company logo
1 of 33
An Introduction to
Functional Programming
     using Haskell
   Michel Rijnders <mies@tty.nl>
Administrativia
Anglais?
tuple programming
 skill level
 working ghci
handouts
breaks
Main Features
purely functional
lazy
higher order
strongly typed
general purpose
History

September 1987 FPCA
“design by committee”
P. Hudak, J. Hughes, S. Peyton Jones, and
P. Wadler: “A History of Haskell: Being
Lazy With Class” (2007)
Main Difference

mainstream languages are all about
state
functional programming is all about
values
Computation

all computation is done via the
evaluation of EXPRESSIONS to yield
VALUES
every value has an associated TYPE
Types

basic types: Char, Bool, Int, Integer,
Double
composite types
  lists: [Char], [Int], [[Char]]
  tuples: (String,Int)
Function Types

square :: Integer -> Integer
(&&) :: Bool -> Bool -> Bool
length :: [a] -> Int
(:) :: a -> [a] -> [a]
Function Definitions
fac :: Int -> Int
fac n = if n == 0
        then 1
        else n * fac (n - 1)
Guards
-- guards
fac’ :: Int -> Int
fac’ n
  | n == 0    = 1
  | otherwise = n * fac’ (n - 1)
Pattern Matching
-- pattern matching
fac’’ :: Int -> Int
fac’’ 0 = 1
fac’’ n = n * fac’’ (n - 1)
Exercises Template
-- file: Main.hs
module Main where

import Prelude hiding (sum,length)

sum :: [Int] -> Int
...

length :: [a] -> Int
...
List Patterns
[]
xs
(x:xs)
(x:_)
(_:xs)
(_:_)
List Comprehensions
> let xs = [2,4,7]
> [ 2 * x | x <- xs ]
[4,8,14]
> [ even x | x <- xs ]
[True,True,False]
> [ 2 * x | x <- xs, even x, x > 3]
[8]
> [ x + y | (x,y) <- [(2,3),(2,1)] ]
[5,3]
> [ x + y | (x,y) <- [(2,3),(2,1)],
x < y ]
[5]
Summary
computation
types
functions
 guards
 pattern matching
list comprehensions
Coming Up

programming with lists
higher-order functions
type classes
algebraic types
List Functions
(:) :: a -> [a] -> [a]      tail, init :: [a] -> [a]

(++) :: [a] -> [a] -> [a]   replicate ::
                            Int -> a -> [a]
(!!) :: [a] -> Int -> [a]
                            take, drop ::
concat :: [[a]] -> [a]      Int -> [a] -> [a]

length :: [a] -> Int        splitAt ::
                            Int -> [a] -> ([a],[a])
head, last :: [a] -> a
More List Functions
repeat :: a -> [a]      and, or ::
                        [Bool] -> Bool
reverse :: [a] -> [a]
                        sum, product ::
zip ::                  [Int] -> Int
[a] -> [b] -> [(a,b)]   [Float] -> Float

unzip ::
[(a,b)] -> ([a],[b])
Programming with Lists
Prelude> :load Sprite
*Sprite> print glider
.#.
..#
###
[(),(),()]
*Sprite> print (flipH glider)
###
..#
.#.
[(),(),()]
Sprite.hs
flipH :: Picture -> Picture
flipH pic = reverse pic

flipV :: Picture -> Picture
flipV pic =
  [ reverse line | line <- pic ]
Higher Order Functions

 functions as arguments
 functions as results
 or both
Higher Order Functions

 patterns of computation
  mapping: transforming elements
  filtering: selecting elements
  folding: combining elements
Mapping and Filtering

 map :: (a -> b) -> [a] -> [b]
 filter :: (a -> Bool) -> [a] -> [a]
 zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
Folding

foldl :: (a -> b -> a) -> a -> [b] -> a
foldl1 :: (a -> b -> a) -> [b] -> a
foldr :: (a -> b -> b) -> b -> [a] -> b
foldr1 :: (a -> a -> a) -> [a] -> a
Type Classes

type class: a collection of of types over
which certain functions are defined
equality class Eq
 (==) :: (Eq a) => a -> a -> Bool
 (/=) :: (Eq a) => a -> a -> Bool
Declaring a Class
class Visible a where
  toString :: a -> String
  size     :: a -> Int

class Eq a where
  (==), (/=) :: a -> a -> Bool
  x /= y = not (x == y)
  x == y = not (x /= y)
Defining an Instance
instance Visible Char where
  toString ch = [ch]
  size _      = 1

instance Eq Bool   where
  True == True     = True
  False == False   = True
  _     == _       = False
Derived Classes
class Eq a => Ord a where
  (<), (<=), (>), (>=) :: a -> Bool
  max, min :: a -> a -> a
  compare :: a -> a -> Ordering
Built-In Classes
Eq
Ord
Enum
Show
Read
Algebraic Types

data Bool = False | True
data Season =
 Spring | Summer | Autumn | Winter
data Ordering = LT | EQ | GT
Product Types
data People = Person Name Age
type Name = String
type Age = Int
data Shape = Circle Double
           | Rectangle Float Float
Recursive Algebraic
         Types
data Expr = Lit Int
          | Add Expr Expr
          | Sub Expr Expr
data Tree a = Node (Tree a) (Tree a)
            | Leaf a
More?

http://haskell.org/
G. Hutton: Programming in Haskell
(Cambridge University Press)
B. O’Sullivan, J. Goerzen, D. Stewart:
Real World Haskell (O’Reilly)

More Related Content

What's hot

Functions and graphs
Functions and graphsFunctions and graphs
Functions and graphs
Sujata Tapare
 
The Algebric Functions
The Algebric FunctionsThe Algebric Functions
The Algebric Functions
itutor
 
5 4 function notation
5 4 function notation5 4 function notation
5 4 function notation
hisema01
 

What's hot (15)

Functions and graphs
Functions and graphsFunctions and graphs
Functions and graphs
 
The Algebric Functions
The Algebric FunctionsThe Algebric Functions
The Algebric Functions
 
Multi dimensional array
Multi dimensional arrayMulti dimensional array
Multi dimensional array
 
Arrays
ArraysArrays
Arrays
 
Lec5
Lec5Lec5
Lec5
 
5 4 function notation
5 4 function notation5 4 function notation
5 4 function notation
 
Lec4
Lec4Lec4
Lec4
 
Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011
 
Functions
FunctionsFunctions
Functions
 
Arrays
ArraysArrays
Arrays
 
Open addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashingOpen addressiing &amp;rehashing,extendiblevhashing
Open addressiing &amp;rehashing,extendiblevhashing
 
Function and Its Types.
Function and Its Types.Function and Its Types.
Function and Its Types.
 
Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4Introduction to matlab lecture 4 of 4
Introduction to matlab lecture 4 of 4
 
Function Basics Math Wiki
Function Basics   Math WikiFunction Basics   Math Wiki
Function Basics Math Wiki
 
Functional programming from its fundamentals
Functional programming from its fundamentalsFunctional programming from its fundamentals
Functional programming from its fundamentals
 

Viewers also liked

Camomile : A Unicode library for OCaml
Camomile : A Unicode library for OCamlCamomile : A Unicode library for OCaml
Camomile : A Unicode library for OCaml
Yamagata Yoriyuki
 
Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)
Anil Madhavapeddy
 
OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法
Hiroki Mizuno
 
OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012
Anil Madhavapeddy
 
Os Peytonjones
Os PeytonjonesOs Peytonjones
Os Peytonjones
oscon2007
 
Real World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみたReal World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみた
blackenedgold
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
Johan Tibell
 
PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法
Yosuke Onoue
 

Viewers also liked (20)

What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...What is Pure Functional Programming, and how it can improve our application t...
What is Pure Functional Programming, and how it can improve our application t...
 
Haskell for the Real World
Haskell for the Real WorldHaskell for the Real World
Haskell for the Real World
 
Scaling Out With Hadoop And HBase
Scaling Out With Hadoop And HBaseScaling Out With Hadoop And HBase
Scaling Out With Hadoop And HBase
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
Camomile : A Unicode library for OCaml
Camomile : A Unicode library for OCamlCamomile : A Unicode library for OCaml
Camomile : A Unicode library for OCaml
 
Using functional programming within an industrial product group: perspectives...
Using functional programming within an industrial product group: perspectives...Using functional programming within an industrial product group: perspectives...
Using functional programming within an industrial product group: perspectives...
 
Ocaml
OcamlOcaml
Ocaml
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)Mirage: ML kernels in the cloud (ML Workshop 2010)
Mirage: ML kernels in the cloud (ML Workshop 2010)
 
Haskell - Functional Programming
Haskell - Functional ProgrammingHaskell - Functional Programming
Haskell - Functional Programming
 
計算数学
計算数学計算数学
計算数学
 
Lispmeetup11
Lispmeetup11Lispmeetup11
Lispmeetup11
 
OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法OCamlでWebアプリケーションを作るn個の方法
OCamlでWebアプリケーションを作るn個の方法
 
OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012OCaml Labs introduction at OCaml Consortium 2012
OCaml Labs introduction at OCaml Consortium 2012
 
Os Peytonjones
Os PeytonjonesOs Peytonjones
Os Peytonjones
 
Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!Hey! There's OCaml in my Rust!
Hey! There's OCaml in my Rust!
 
Real World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみたReal World OCamlを読んでLispと協調してみた
Real World OCamlを読んでLispと協調してみた
 
High-Performance Haskell
High-Performance HaskellHigh-Performance Haskell
High-Performance Haskell
 
関数型プログラミング入門 with OCaml
関数型プログラミング入門 with OCaml関数型プログラミング入門 with OCaml
関数型プログラミング入門 with OCaml
 
PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法PythonistaがOCamlを実用する方法
PythonistaがOCamlを実用する方法
 

Similar to An Introduction to Functional Programming using Haskell

Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
Vasil Remeniuk
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
djspiewak
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
league
 

Similar to An Introduction to Functional Programming using Haskell (20)

Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Scala Functional Patterns
Scala Functional PatternsScala Functional Patterns
Scala Functional Patterns
 
Power of functions in a typed world
Power of functions in a typed worldPower of functions in a typed world
Power of functions in a typed world
 
Algebraic Data Types and Origami Patterns
Algebraic Data Types and Origami PatternsAlgebraic Data Types and Origami Patterns
Algebraic Data Types and Origami Patterns
 
An overview of Python 2.7
An overview of Python 2.7An overview of Python 2.7
An overview of Python 2.7
 
A tour of Python
A tour of PythonA tour of Python
A tour of Python
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 
Scalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with ScalaScalapeno18 - Thinking Less with Scala
Scalapeno18 - Thinking Less with Scala
 
Practical cats
Practical catsPractical cats
Practical cats
 
Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!Python 101++: Let's Get Down to Business!
Python 101++: Let's Get Down to Business!
 
Oh, All the things you'll traverse
Oh, All the things you'll traverseOh, All the things you'll traverse
Oh, All the things you'll traverse
 
(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads(2015 06-16) Three Approaches to Monads
(2015 06-16) Three Approaches to Monads
 
sonam Kumari python.ppt
sonam Kumari python.pptsonam Kumari python.ppt
sonam Kumari python.ppt
 
Fp in scala with adts part 2
Fp in scala with adts part 2Fp in scala with adts part 2
Fp in scala with adts part 2
 
High Wizardry in the Land of Scala
High Wizardry in the Land of ScalaHigh Wizardry in the Land of Scala
High Wizardry in the Land of Scala
 
Intro.ppt
Intro.pptIntro.ppt
Intro.ppt
 
The Essence of the Iterator Pattern
The Essence of the Iterator PatternThe Essence of the Iterator Pattern
The Essence of the Iterator Pattern
 
Modular Module Systems
Modular Module SystemsModular Module Systems
Modular Module Systems
 
The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)The Essence of the Iterator Pattern (pdf)
The Essence of the Iterator Pattern (pdf)
 
Munihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template HaskellMunihac 2018 - Beautiful Template Haskell
Munihac 2018 - Beautiful Template Haskell
 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
Earley Information Science
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
 

Recently uploaded (20)

Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 

An Introduction to Functional Programming using Haskell

Editor's Notes

  1. Introductie Eduard
  2. - FPCA: Conference on Functional Programming Languages and Computer Architecture - Paul Hudak: Yale - John Hughes: Chalmers (G&amp;#xF6;teborg) - Simon Peyton Jones: Microsoft Cambridge - Philp Wadler: Edinburgh
  3. - ghci demo - exercises (1. Expressions, Values, and Types)
  4. - exercises 2
  5. - exercises 3 - warn about repeated variables
  6. - generator - one or more tests - pattern - exercises 4
  7. - exercises 5
  8. - exercises 6
  9. - constraints
  10. - default definitions
  11. - exercises 8