Docsity

Haskell Wrap Up - Organization of Programming Languages - Lecture Notes, Study notes of Programming Languages

Partial preview of the text

Download Haskell Wrap Up - Organization of Programming Languages - Lecture Notes and more Study notes Programming Languages in PDF only on Docsity!

Today ...

  • Higher-Order Functions (cont.)
  • Lambda Expressions (if time)
  • Haskell wrap up

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]

Wrapping up

What we didn’t cover

  • A lot! ... Haskell has many features
  • The IO type (recall “purity”)
    • Various I/O operations
    • All the normal things you’d expect to write real apps
  • Monads
    • A pattern (data type) to chain together a list of operations
    • Functional machinery to sequence commands
    • In homework, the do expression is an example do input <- readFile inputFile putStrLn (function input)
    • Appears imperative, but is still functional
  • The Monad typeclass has a sequencing function >>=

Prelude> :type (>>=) (>>=) :: (Monad m) => m a -> (a -> m b) -> m b

  • extracts (unwraps) value on left from the Monad
  • passes it to a function that returns a wrapped result value