CS 330 Lecture 25 – List Comprehensions and Map
Agenda
- what ?s
- program this
- list comprehensions
- translating between list comprehensions and imperative loops
- map
Note
We start with a Program This to gel our understanding of Haskell function structure:
Write function
indexOf
, which accepts a needle and haystack, and returns where the needle lives in the haystack. If the needle can’t be found, it returns -1.
From there we jump into discussing list comprehensions. We start by looking at the template of imperative code that they replace. We introduce the notion of a predicate. We’ll solve a handful of list comprehension problems together.
As we have time, we’ll examine a special case of a list comprehension, one with no predicates and involving only one list. This pattern is called a map, not to be confused with the abstract data type (though the underlying idea of mapping one value to another is the same). We’ll write our own map'
function that accepts a transformation function and a list as input.
Code
…