Your SlideShare is downloading. ×
An Introduction to Functional Programming using Haskell
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

An Introduction to Functional Programming using Haskell

3,593

Published on

Slides of the workshop I gave at Devnology's Community Day.

Slides of the workshop I gave at Devnology's Community Day.

Published in: Technology, Education
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,593
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
80
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide
  • Introductie Eduard
  • - FPCA: Conference on Functional Programming Languages and Computer Architecture
    - Paul Hudak: Yale
    - John Hughes: Chalmers (Göteborg)
    - Simon Peyton Jones: Microsoft Cambridge
    - Philp Wadler: Edinburgh
  • - ghci demo
    - exercises (1. Expressions, Values, and Types)
  • - exercises 2
  • - exercises 3
    - warn about repeated variables
  • - generator
    - one or more tests
    - pattern
    - exercises 4
  • - exercises 5
  • - exercises 6
  • - constraints
  • - default definitions
  • - exercises 8
  • Transcript

    • 1. An Introduction to Functional Programming using Haskell Michel Rijnders <mies@tty.nl>
    • 2. Administrativia Anglais? tuple programming skill level working ghci handouts breaks
    • 3. Main Features purely functional lazy higher order strongly typed general purpose
    • 4. 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)
    • 5. Main Difference mainstream languages are all about state functional programming is all about values
    • 6. Computation all computation is done via the evaluation of EXPRESSIONS to yield VALUES every value has an associated TYPE
    • 7. Types basic types: Char, Bool, Int, Integer, Double composite types lists: [Char], [Int], [[Char]] tuples: (String,Int)
    • 8. Function Types square :: Integer -> Integer (&&) :: Bool -> Bool -> Bool length :: [a] -> Int (:) :: a -> [a] -> [a]
    • 9. Function Definitions fac :: Int -> Int fac n = if n == 0 then 1 else n * fac (n - 1)
    • 10. Guards -- guards fac’ :: Int -> Int fac’ n | n == 0 = 1 | otherwise = n * fac’ (n - 1)
    • 11. Pattern Matching -- pattern matching fac’’ :: Int -> Int fac’’ 0 = 1 fac’’ n = n * fac’’ (n - 1)
    • 12. Exercises Template -- file: Main.hs module Main where import Prelude hiding (sum,length) sum :: [Int] -> Int ... length :: [a] -> Int ...
    • 13. List Patterns [] xs (x:xs) (x:_) (_:xs) (_:_)
    • 14. 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]
    • 15. Summary computation types functions guards pattern matching list comprehensions
    • 16. Coming Up programming with lists higher-order functions type classes algebraic types
    • 17. 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
    • 18. 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])
    • 19. Programming with Lists Prelude> :load Sprite *Sprite> print glider .#. ..# ### [(),(),()] *Sprite> print (flipH glider) ### ..# .#. [(),(),()]
    • 20. Sprite.hs flipH :: Picture -> Picture flipH pic = reverse pic flipV :: Picture -> Picture flipV pic = [ reverse line | line <- pic ]
    • 21. Higher Order Functions functions as arguments functions as results or both
    • 22. Higher Order Functions patterns of computation mapping: transforming elements filtering: selecting elements folding: combining elements
    • 23. Mapping and Filtering map :: (a -> b) -> [a] -> [b] filter :: (a -> Bool) -> [a] -> [a] zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
    • 24. 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
    • 25. 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
    • 26. 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)
    • 27. Defining an Instance instance Visible Char where toString ch = [ch] size _ = 1 instance Eq Bool where True == True = True False == False = True _ == _ = False
    • 28. Derived Classes class Eq a => Ord a where (<), (<=), (>), (>=) :: a -> Bool max, min :: a -> a -> a compare :: a -> a -> Ordering
    • 29. Built-In Classes Eq Ord Enum Show Read
    • 30. Algebraic Types data Bool = False | True data Season = Spring | Summer | Autumn | Winter data Ordering = LT | EQ | GT
    • 31. Product Types data People = Person Name Age type Name = String type Age = Int data Shape = Circle Double | Rectangle Float Float
    • 32. Recursive Algebraic Types data Expr = Lit Int | Add Expr Expr | Sub Expr Expr data Tree a = Node (Tree a) (Tree a) | Leaf a
    • 33. More? http://haskell.org/ G. Hutton: Programming in Haskell (Cambridge University Press) B. O’Sullivan, J. Goerzen, D. Stewart: Real World Haskell (O’Reilly)

    ×