teaching machines

CS 330 Lecture 31 – Maps, Filters, and Folds

April 22, 2015 by . Filed under cs330, lectures, spring 2015.

Agenda

TODO

Problems

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