Functional Programming Lecture 12 - more higher order functions - PowerPoint PPT Presentation

About This Presentation
Title:

Functional Programming Lecture 12 - more higher order functions

Description:

The function unzip 'unzips' a list of pairs to produce a pair of lists. ... What is zip (unzip zs)? = type error, unzip has produced a tuple! ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 9
Provided by: muffyc
Category:

less

Transcript and Presenter's Notes

Title: Functional Programming Lecture 12 - more higher order functions


1
Functional ProgrammingLecture 12 - more higher
order functions
2
Recursion over two arguments
  • The function zip zips together two lists, to
    produce a new list of pairs.
  • zip a -gt b -gt (a,b)
  • zip (xxs) (yys) (x,y) zip xs ys
  • zip _ _
  • or
  • zip (xxs) (yys) (x,y) zip xs ys
  • zip (yys)
  • zip xs
  • (Why not zip (xxs) ?)
  • E.g. zip 1,2,3 a,b,c gt (1,a),
    (2,b), (3,c)
  • zip 1,2,3 a,b gt (1,a),
    (2,b)

3
  • The function unzip unzips a list of pairs to
    produce a pair of lists.
  • unzip (a,b) -gt (a,b)
  • tricky definition
  • unzip (,)
  • unzip (x,y)zs (xxs,yys)
  • where (xs,ys) unzip zs
  • What is unzip (1,a), (2,b), (3,c)?
  • gt (1,2,3,a,b,c)
  • What is unzip (zip xs ys) ?
  • gt (xs,ys) if xs and ys have same
    length
  • What is zip (unzip zs)?
  • gt type error, unzip has produced a
    tuple!

4
Fold and Foldr
  • Consider computing the sum of the elements of a
    list
  • e1, e2, en i.e. e1 (e2 ( en )).
  • sumlist Int -gt Int
  • sumlist x x
  • sumlist (xxs) x sumlist xs
  • Now consider computing the maximum of the
    elements of a list e1, e2, en
  • i.e. e1 max (e2 max ( max en)).
  • maxlist Int -gt Int
  • maxlist x x
  • maxlist (xxs) max x (maxlist xs)
  • In both cases, we are folding the binary
    function and max through a non-empty list.

5
Fold
  • Define fold as the function
  • fold (a -gt a -gt a) -gt a -gt a
  • a binary function a list the result
  • fold f x x
  • fold f (xxs) f x (fold f xs)
  • Note although x matches the second equation,
    we will always apply the first equation.
  • Examples
  • fold () False, True, False
  • gt True
  • fold () Hello , world, !
  • gt Hello world!
  • fold () 1..5
  • gt 120 n.b infix op. Becomes
    prefix in ( )

6
  • But fold has not been defined for an empty list.
  • What would we like to define
  • fold () and
  • fold () as ?
  • There is no general answer, we must provide one
    for each function we give to fold.
  • The function which defines fold for an empty list
    is foldr.

7
Foldr
  • Define foldr as the function
  • foldr (a -gt b -gt b) -gt b -gt a -gt b
  • a binary function empty value a list
    the result
  • Note foldr has a more general type.
  • foldr f st st
  • foldr f st (xxs) f x (foldr f st xs)
  • Note foldr means fold right. Usually the extra
    argument is the identity element, i.e. the
    stopping value st is the identity for f.
  • foldr is defined in the standard prelude.

8
  • Examples
  • foldr () False False, True, False
  • gt True
  • foldr () True True, True
  • gt True
  • foldr () False True, True
  • gt False
  • foldr () 1 1..5
  • gt 120 1 is an identity
    for
  • foldr () 0 1..5
  • gt 0 because 0 is a
    zero for
  • fac n foldr () 1 1..n
Write a Comment
User Comments (0)
About PowerShow.com