Docsity

Lecture Notes for CPSC 326 (Spring 2013) on Functional Programming, Study notes of Programming Languages

Partial preview of the text

Download Lecture Notes for CPSC 326 (Spring 2013) on Functional Programming and more Study notes Programming Languages in PDF only on Docsity!

Today ...

  • Comments on pattern matching and guards
  • Function Composition
  • Higher-Order Functions
  • Lambda Expressions (if time)
  • Fast (recruiters)

Homework ...

  • HW & Reading due
  • HW 6 & Reading 7 out

Reminders ...

  • Switching gears to Prolog (thurs.)
  • Exam 2 next Thurs (3/7)

Warmup: The removeDups function

Lets say we wanted to remove all the repeating items: Prelude> removeDups [1,2,2,2,3] [1, 2, 3]

  • We can do this using recursion ... removeDups [] = [] removeDups [x] = [x] removeDups (x:y:zs) | x == y = removeDups (x:zs) | otherwise = x : removeDups (y:zs)
  • Why is the second pattern needed?
  • Could we swap the order of the patterns? ... YES!

Hints on patterns and guards

If two identical patterns, use guards myElem n (x:xs) | n == x = True myElem n (x:xs) = myElem n xs

Q: How would we write this as one pattern?

Use otherwise for efficiency ... myElem n (x:xs) | n == x = True | n /= x = myElem n xs

Q: Why is using otherwise better here?

Q: What about this expression?

Prelude> :type head. words head. words :: String -> String

Prelude> (head. words) "the quick brown fox" the

Q: What about this expression?

Prelude> :type words. head words. head :: [String] -> [String]

Prelude> (words. head) ["the quick brown fox"] ["the", "quick", "brown", "fox"]

Q: What about this expression?

Prelude> :type tail. head tail. head :: [[a]] -> [a]

Q: And this expression?

Prelude> :type head. length ... error ...

  • here the types do not align!

Higher order functions

A higher order function ...

  • is a function that takes functions as arguments ... focus of today
  • or returns a function as a result ... we’ve seen this w/ partial application

Function compostion is a higher-order function

  • some others: filter, map, zip, zipWith, foldl, foldr

Another example: filter

filter :: (a -> Bool) -> [a] -> [a]

  • takes a predicate and a list
  • returns a new list of elements
  • each element satisfies the predicate

Prelude> :type odd odd :: (Integral a) => a -> Bool

Prelude> filter odd [1 .. 10] [1, 3, 5, 7, 9]

Merging two lists with zip

zip :: [a] -> [b] -> [(a, b)]

Prelude> zip [1, 2, 3] "abc" [(1, ’a’), (2, ’b’), (3, ’c’)]

Often zipWith more useful ...

zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]

  • What is (a -> b -> c)?
    • A 2-argument function that returns a single value
    • Applied to “pairs” of [a] and [b] to produce [c]

Prelude> zipWith (+) [1,2,3] [4,5,6] [5,7,9]

Accumulating values (from the left) with foldl

foldl :: (a -> b -> a) -> a -> [b] -> a

  • (a -> b -> a) is the step function
  • first a is an accumulator
  • [b] are the input values
  • last a is the accumulated value
  • new accumulator = step function accumulator b value

Prelude> foldl (+) 0 [1, 2, 3] 6

  • Note this is just sumL

How it works ...

foldl _ acc [] = acc foldl step acc (x:xs) = foldl step (step acc x) xs

foldl (-) 9 [5, 3, 1] → foldl (-) (9 - 5) [3, 1] → foldl (-) ((9 - 5) - 3) [1] → foldl (-) (((9 - 5) - 3) - 1) [] → (((9 - 5) - 3) - 1) → 0

Many recursive functions follow the fold pattern

filter :: (a -> Bool) -> [a] -> [a] filter p [] = [] filter p (x:xs) | p x = x : filter p xs | otheriwse = filter p xs

Q: How can filter be defined using foldr? filter’ p xs = foldr step [] xs where step x acc | p x = x : acc | otheriwse = acc

  • For example ... filter’ odd [1,2,3] → foldr step [] [1,2,3] → step 1 (foldr step [] [2,3]) → 1 : (foldr step [] [2,3]) → 1 : (step 2 (foldr step [] [3])) → 1 : (foldr step [] [3]) → 1 : (step 3 foldr step [] []) → 1 : (3 : (foldr step [] [])) → 1 : (3 : [])

Q: How can filter be defined using foldl? filter’ p xs = foldl step [] xs where step acc x | p x = acc ++ [x] | otherwise = acc

We can also define map using foldr

map’ :: (a -> b) -> [a] -> [b] map’ f xs = foldr step [] xs where step x ys = f x : ys

For example ...

map’ odd [1,2,3] → foldr step [] [1,2,3] → step 1 (foldr step [] [2,3]) → odd 1 : (foldr step [] [2,3]) → odd 1 : (step 2 (foldr step [] [3])) → odd 1 : (odd 2 : (foldr step [] [3])) → odd 1 : (odd 2 : (step 3 (foldr step [] []))) → odd 1 : (odd 2 : (odd 3 : (foldr step [] []))) → odd 1 : (odd 2 : (odd 3 : []))

Why care about these higher-order functions?

  • In general, should use them whenever possible ...
  • ... Can make functions easier to understand (shorter)
  • ... Well behaved (fewer bugs)
  • ... Optimization

Instead, we can use lambda expressions (“anonymous” functions)

Prelude> ((x, y, z) -> x) (’a’, ’b’, ’c’) ’a’

Prelude> ((x, y, z) -> y) (’a’, ’b’, ’c’) ’b’

  • We are defining the function “on the fly”
  • So these are the same (but the second isn’t named):

fst3 (x, y, z) = x (x, y, z) -> x

  • And they have exactly the same types:

Prelude> :type fst fst3 :: (a, b, c) -> a

Prelude> :type (x, y, z) -> x (x, y, z) -> x :: (a, b, c) -> a

Lambda expressions can be useful w/ higher-order functions:

Prelude> zipWith (\x y -> x^2+y^2) [1..10] [10,9..1] [101,85,73,65,61,61,65,73,85,101]