{- Max Sherman CSE 341 Section 3 todo: 1. practice writing functions 2. currying 3. types -} -- practice -- (me) flip: list of 2-tuples -> same list with elts in tuples switched -- anonymous function example (me) -- write flip using map -- myzip (them) myzip xs ys = if null xs || null ys then [] else (head xs, head ys) : myzip (tail xs) (tail ys) -- (them) filter sum list -> take in list and predicate, only sum #'s for which predicate is true -- currying -- re-write zip to do explicit currying zip_realtalk xs = \ ys -> if null xs || null ys then [] else (head xs, head ys) : zip_realtalk (tail xs) (tail ys) -- what is the type of zip? -- here is a multi argument function, is it curried, and what is its type? add5things a b c d e = a + b + c + d + e -- here is another function add3things a b c = a + b + c -- what is v defined below? v = add3things 2 {- DONT DO -- why is this useful, consider fold myfold f b [] = b myfold f b (x:xs) = f x (myfold f b xs) -- for a list a1 : a2 : a3 : a4 : [] -- fold will make this look like: -- f(a1, f(a2, f(a3... b))))))) etc -- we can use this like sumlist = myfold (+) 0 -- which is a partial application, and then sum lists with the sumlist function DONT DO -} -- Types -- expressions have types, 3, "pizza", True, 4 + 3, f 4, etc -- functions are expressions (because they are values), so they have types. -- These are called arrow types -- what is the type of add1 x = x + 1 -- what is type of (+) --what is type of mystery a b c = a + (if b then 1 else c) -- what is the type of reverse, quadratic formula function -- DONT DO COMPOSE -- consider the compose function: compose f g x = f (g x) -- we can use it to compose like (x+1) and (2x) for example mycomp = compose (\x -> x + 1) (\x -> 2 * x) -- what is it's type