# teaching machines

## CS 330 Lecture 32 – Type Inference, Functions, and Lists

April 18, 2014 by . Filed under cs330, lectures, spring 2014.

### Agenda

• what ?s
• type interference in other languages
• type signatures
• curried functions
• infinite lists
• list processing
• case statements
• pattern matching
• map
• lambda

Someone hands you a number k and an arbitrary list of size n. The someone demands, “What’s the kth smallest element?” Assume 0 <= k < n. What do you do?

Someone hands you a number k. The someone demands, “What’s the kth smallest element?” What do you do?

### Code

#### april18.hs

import Data.List

initials :: String -> String -> String -> String

kth :: Int -> [Int] -> Int
kth k items = sort items !! k

best :: [Int] -> Int
best = kth 0

runnerup :: [Int] -> Int
runnerup = kth 1

sum' :: [Int] -> Int
-- sum' ns =
-- case ns of
-- [] -> 0
-- (first:rest) -> first + sum' rest
sum' [] = 0
sum' (first:rest) = first + sum' rest

convertMilesToKilometers [] = []
convertMilesToKilometers (first:rest) = (1.609 * first) : convertMilesToKilometers rest

needsOurLove :: [(String, String)] -> [Bool]
needsOurLove [] = []
needsOurLove ((_, lastName):rest) = (head lastName >= 'N') : needsOurLove rest

### Haiku

Nerds learn fast, mostly
Part of Haskell trips them up
The social functions