CS 330 Lecture 32 – Type Inference, Functions, and Lists
- what ?s
- type interference in other languages
- type signatures
- curried functions
- infinite lists
- list processing
- case statements
- pattern matching
Think About This
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?
import Data.List initials :: String -> String -> String -> String initials first second third = head first : head second : head third :  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
Part of Haskell trips them up
The social functions