CS 330 Lecture 33 – Map, Fold, Filter, Lambdas

Agenda

  • what ?s
  • program this
  • three patterns
    • for-each
    • combine
    • filter
  • anonymous functions

TODO

Program This

  • Write function quote, which accepts a list of Strings 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.

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

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *