SlideShare a Scribd company logo
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

PPTX
Algebraic functions powerpoint
PDF
Introduction to haskell
PDF
Functional programming with haskell
PPT
Sparse Matrix and Polynomial
PDF
PPTX
PPT
Arrays
Algebraic functions powerpoint
Introduction to haskell
Functional programming with haskell
Sparse Matrix and Polynomial
Arrays

What's hot (15)

PPS
Functions and graphs
PPTX
The Algebric Functions
PDF
Multi dimensional array
PPTX
5 4 function notation
PDF
Functional Programming and Haskell - TWBR Away Day 2011
PPTX
Functions
PPT
PPTX
Open addressiing &amp;rehashing,extendiblevhashing
DOCX
Function and Its Types.
PPTX
Introduction to matlab lecture 4 of 4
KEY
Function Basics Math Wiki
PDF
Functional programming from its fundamentals
Functions and graphs
The Algebric Functions
Multi dimensional array
5 4 function notation
Functional Programming and Haskell - TWBR Away Day 2011
Functions
Open addressiing &amp;rehashing,extendiblevhashing
Function and Its Types.
Introduction to matlab lecture 4 of 4
Function Basics Math Wiki
Functional programming from its fundamentals
Ad

Viewers also liked (20)

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

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

PDF
Introduction to Functional Languages
PDF
Why Haskell Matters
PPT
haskell5.ppt is a marketing document lol
PDF
01. haskell introduction
PDF
[FLOLAC'14][scm] Functional Programming Using Haskell
PDF
02. haskell motivation
PDF
Functional programming using haskell notes iitk
PDF
The Next Great Functional Programming Language
PDF
10. haskell Modules
PDF
07. haskell Membership
KEY
Programming haskell chapter10
PDF
Introduction to Functional Programming with Haskell and JavaScript
PDF
Functional Programming
PPTX
Introduction to Haskell: 2011-04-13
PDF
Functional Programming by Examples using Haskell
ODP
06. haskell type builder
PPT
Intro.ppt
PDF
Haskell - Being lazy with class
PPTX
Functional programming seminar (haskell)
Introduction to Functional Languages
Why Haskell Matters
haskell5.ppt is a marketing document lol
01. haskell introduction
[FLOLAC'14][scm] Functional Programming Using Haskell
02. haskell motivation
Functional programming using haskell notes iitk
The Next Great Functional Programming Language
10. haskell Modules
07. haskell Membership
Programming haskell chapter10
Introduction to Functional Programming with Haskell and JavaScript
Functional Programming
Introduction to Haskell: 2011-04-13
Functional Programming by Examples using Haskell
06. haskell type builder
Intro.ppt
Haskell - Being lazy with class
Functional programming seminar (haskell)

Recently uploaded (20)

PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PDF
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
PPTX
Cloud computing and distributed systems.
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
Approach and Philosophy of On baking technology
PDF
KodekX | Application Modernization Development
PDF
Machine learning based COVID-19 study performance prediction
PPTX
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PPTX
MYSQL Presentation for SQL database connectivity
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Encapsulation_ Review paper, used for researhc scholars
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Reach Out and Touch Someone: Haptics and Empathic Computing
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Bridging biosciences and deep learning for revolutionary discoveries: a compr...
Cloud computing and distributed systems.
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
“AI and Expert System Decision Support & Business Intelligence Systems”
Approach and Philosophy of On baking technology
KodekX | Application Modernization Development
Machine learning based COVID-19 study performance prediction
PA Analog/Digital System: The Backbone of Modern Surveillance and Communication
Network Security Unit 5.pdf for BCA BBA.
Per capita expenditure prediction using model stacking based on satellite ima...
MYSQL Presentation for SQL database connectivity
20250228 LYD VKU AI Blended-Learning.pptx
Encapsulation_ Review paper, used for researhc scholars
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Advanced methodologies resolving dimensionality complications for autism neur...
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...

An Introduction to Functional Programming using Haskell

Editor's Notes

  • #2: Introductie Eduard
  • #5: - 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
  • #8: - ghci demo - exercises (1. Expressions, Values, and Types)
  • #13: - exercises 2
  • #14: - exercises 3 - warn about repeated variables
  • #15: - generator - one or more tests - pattern - exercises 4
  • #21: - exercises 5
  • #24: - exercises 6
  • #26: - constraints
  • #27: - default definitions
  • #28: - exercises 8