# GC163011 Functional Programming Lecture 5 Miranda

Document Sample

```					GC16/3011 Functional Programming
Lecture 5

Miranda
patterns, functions, recursion and lists

5/31/2010                 1
Contents

   Offside rule / where blocks / evaluation
   Partial / polymorphic functions
   Patterns and pattern-matching
   Recursive functions
   Lists
 Functions using lists
 Recursive functions using lists

5/31/2010              2
Functions

   The offside rule and “where” blocks

   Mapping from source to target type domains

   Application associates to the left!

   Function evaluation
 normal order, lazy
 main = fst (34, (23 / 0) )

5/31/2010          3
Functions

   Partial functions
 those which have no result (i.e. return an error) for
some valid values of valid input type
 f (x,y) = x / y

   Polymorphic functions
 fst, snd
 g (x,y) = (-y, x)
 three x = 3

5/31/2010                        4
Patterns

   Tuple patterns
 (x,y,z) = (3, “hello”, (34,True,[3]))
   as a test
   as a definition

   Patterns for function definitions
not True = False
not False = True

   Top-down evaluation semantics
f   3 = 45
f 489 = 3
f any = 345*219
5/31/2010        5
Patterns

   Non-exhaustive patterns
f True = False

   Patterns can destroy laziness
fst (x,0) = x
fst (x,y) = x

   Can a pattern contain a redex?
   No! A pattern must be a constant expression
   (special exception – “(n + 1)” in Miranda)

   Duplicate parameter names

5/31/2010                6
Recursive Functions

   Recursion is the ONLY way to program loops in a
functional language
 Function calls itself inside its own body

   Very powerful – very flexible

   In imperative languages, can be slow

   In functional languages, highly optimised

5/31/2010                 7
Recursive Functions

   Beware:
loop_forever x = loop_forever x

   Must have:
 Terminating condition
 Changing argument
 …that converges on the terminating condition!
f :: num -> [char]
f 0 = “”
f n = “X” ++ (f (n – 1))

5/31/2010               8
Recursive Functions

   Stack recursion
     f 3
    “X” ++ (f (3 – 1))
    “X” ++ (f 2)
    “X” ++ (“X” ++ (f (2-1)))
    “X” ++ (“X” ++ (f 1))
    “X” ++ (“X” ++ (“X” ++ (f (1-1))))
    “X” ++ (“X” ++ (“X” ++ “”))
    “X” ++ (“X” ++ “X”)
    “X” ++ “XX”
    “XXX”

5/31/2010      9
Recursive Functions

   Accumulative recursion
plus :: (num, num) -> num
plus (x, 0) = x
plus (x, y) = plus (x+1, y-1)

5/31/2010      10
Type Synonyms

   f :: (([char],num,[char]),(num,num,num)) -> bool

   str = = [char]
   coord = = (num, num, num)

   f :: ((str,num,str), coord) -> bool

5/31/2010                      11
LISTS

   Lists are another way to collect together
related data
   But lists are special - they are RECURSIVE
   Data can be recursive, just like functions!

5/31/2010                   12
LISTS

   A list of type a is either:
 Empty, or
 An element of type a together with a list of
elements of the same type
   List of num:
 []
 (34: [])                              [34]
 (34: (13: []))                    [34, 13]

5/31/2010                    13
Functions using lists

bothempty :: ([*],[**]) -> bool
bothempty ([], []) = True
bothempty anything = False

myhd :: [*] -> *
myhd [] = error “take head of empty list”
myhd (x : rest) = x
5/31/2010              14
Recursive functions using lists

sumlist :: [num] -> num
sumlist [] = 0
sumlist (x : rest) = x + (sumlist rest)

length :: [*] -> num
length [] = 0
length (x : rest) = 1 + (length rest)
5/31/2010           15
Exercise

   Can you write a function “threes” which
takes as input a list of whole numbers and
produces as output a count of how many 3s
occur in the input?

5/31/2010                  16
Summary

   Offside rule / where blocks / evaluation
   Partial / polymorphic functions
   Patterns and pattern-matching
   Recursive functions
   Lists
 Functions using lists
 Recursive functions using lists

5/31/2010              17

```
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
 views: 14 posted: 6/1/2010 language: English pages: 17
How are you planning on using Docstoc?