CS 330 Lecture 33 – Map, Fold, Filter, Lambdas
Agenda
- what ?s
- program this
- three patterns
- for-each
- combine
- filter
- anonymous functions
TODO
- Read chapter 6 in Learn You a Haskell (http://learnyouahaskell.com/higher-order-functions). 1/4 sheet.
Program This
- Write function
quote
, which accepts a list ofString
s and wraps each in double-quotes. - Write function
all
, which accepts a list ofBool
and reports whether all of the elements is true. - Write function
bigs
, which accepts a list ofInt
and returns a new list containing just the elements that are greater than 100.
Code
april21.hs
data Month = January | February | March | April | May | June | July | August | September | October | November | December deriving (Enum, Show, Read, Bounded)
-- quote sts = ["\"" ++ st ++ "\"" | st <- sts]
convertMilesToKilometers [] = []
convertMilesToKilometers (first:rest) = (1.609 * first) : convertMilesToKilometers rest
quote [] = []
quote (first:rest) = ("\"" ++ first ++ "\"") : quote rest
map' f [] = []
map' f (first:rest) = f first : map' f rest
quoteOne s = "\"" ++ s ++ "\""
quote' strings = map' quoteOne strings
mult10 ns = map' (\n -> 10 * n) ns
bigs [] = []
bigs (first:rest)
| first > 100 = first : bigs rest
| otherwise = bigs rest
filter' f [] = []
filter' f (first:rest)
| f first = first : filter' f rest
| otherwise = filter' f rest
Haiku
Nerds learn fast, mostly
Part of Haskell trips them up
The social functions
Part of Haskell trips them up
The social functions