CS 330 Lecture 33 – Map, Fold, Filter, Lambdas
April 21, 2014 by Chris Johnson. Filed under cs330, lectures, spring 2014.
Agenda
- what ?s
- program this
- three patterns
- anonymous functions
TODO
Program This
- Write function
quote
, which accepts a list of String
s and wraps each in double-quotes.
- Write function
all
, which accepts a list of Bool
and reports whether all of the elements is true.
- Write function
bigs
, which accepts a list of Int
and returns a new list containing just the elements that are greater than 100.
show
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
show