CS 330 Lecture 31 – Maps, Filters, and Folds
April 22, 2015 by Chris Johnson. Filed under cs330, lectures, spring 2015.
Agenda
 what ?s
 a gallery of maps, filters, and folds
 pairs
 lambdas
 composition
TODO
 On a 1/4 sheet, write these functions using
map
, filter
, or fold
:

lengths
, which accepts a list of strings and yields a list of all their lengths. For example, lengths ["a", "bcd", "efghi"]
yields [1, 3, 5]
.

tweens
, which accepts a low, a high, and a list of numbers. It yields a list of all numbers between low and high, inclusive. For example, tweens 13 19 [5, 13, 18, 25]
yields [13, 18]
.
seconds
, which accepts a list of pairs and returns a list of only the second elements. For example, seconds [("chris", 0), ("matt", 17), ("jacob", 10), ("caitlin", 23)]
yields [0, 17, 10, 23]
.
Problems
 Write a function
resort
that accepts a list and yields the list sorted in reverse. Use sort
and reverse
.
 Write a function
expand
that accepts a list of stringn pairs. It yields a list in which each pair has been expanded into a list comprised of n instances of its string. For example: expand [("dog", 2), ("cat", 3")]
yields [["dog", "dog"], ["cat", "cat", "cat"]]
.
 Write a function
proportionalize
that accepts a list of integer weights. It returns a list of the weights, proportionalized. For example, proportionalize [1, 3, 4]
yields [0.125, 0.375, 0.5]
.
 Write a function
scalebias
that accepts a scale, a bias, and a list of numbers. It yields a list where all elements have been scaled by the scale factor and offset by the bias.
 Write a function
nonzeroes
that accepts a list of numbers and yields a list containing only the nonzero elements. For example, nonzero [0, 1, 3, 0]
yields [1, 3]
.
 Write a function
overs
that accepts an n and a list of strings and yields a list containing only those strings whose length is greater than n. For example, overs 3 ["the", "quick", "brown", "fox"]
yields ["quick", "brown"]
.
 Write a function
all
that accepts a list of booleans and yields true if all elements are true.
 Write a function
any
that accepts a list of booleans and yields true if any element is true.
 Write a function
sum
that accepts a list of numbers and yields their sum.
 Write a function
product
that accepts a list of numbers and yields their product.
 Write a function
length
that accepts a list and yields its length.
 Write a function
join
that accepts a list of strings and yields their concatenation.
 Write a function
max
that accepts a list of numbers and yields their maximum.
Code
Hof.hs
import Data.List
resort :: Ord a => [a] > [a]
resort = (reverse . sort)
 pairexpander :: (String, Int) > [String]
 pairexpander (s, n) = replicate n s
expand :: [(String, Int)] > [[String]]
 expand list = map pairexpander list
 expand list = map (\(s, n) > replicate n s) list
expand = map (\(s, n) > replicate n s)
proportionalize :: [Double] > [Double]
 proportionalize list = map (\n > n / total) list
 where total = sum list
proportionalize list = map (/ total) list
where total = sum list
all' :: [Bool] > Bool
all' [] = True
all' (first:rest) = first && all' rest
any' :: [Bool] > Bool
any' [] = False
any' (first:rest) = first  any' rest
sum' :: [Double] > Double
sum' [] = 0
sum' (first:rest) = first + sum' rest
foldr' mix accum [] = accum
foldr' mix accum (first:rest) = foldr' mix (mix accum first) rest
all'' = foldr' (&&) True
any'' = foldr' () False
sum'' = foldr' (+) 0
product'' = foldr' (*) 1
join'' = foldr' (++) ""
Haiku
on productivity
I outsourced my job
To map and filter and fold
Not bad for a cat
Leave a Reply