{- Question 1 -} concat' :: [[a]] -> [a] concat' = foldr (++) [] -- concat' ls = fofldr (++) [] ls -- this will also work {- Question 2 -} data Point = Point Double Double deriving Show --or --data Point = Point {x :: Double, y :: Double} deriving Show distanceBetween :: Point -> Point -> Double distanceBetween (Point x1 y1) (Point x2 y2) = sqrt((x1 - x2)^2 + (y1 - y2)^2) --or, with above alternate data Point type --distanceBetween p1 p2 = sqrt((x p1 - x p2)^2 + (y p1 - y p2)^2) shiftPoints :: [Point] -> Point -> [Point] shiftPoints plst (Point x2 y2) = map (\(Point x1 y1) -> Point (x1+x2) (y1+y2)) plst totalPath :: [Point] -> Double totalPath ls = case ls of [] -> 0 [x] -> 0 (x1:x2:xs) -> distanceBetween x1 x2 + totalPath (x2:xs) {- Question 3 -} -- type of the == operator -- :t (==) -- (==) :: Eq a => a -> a -> Bool -- the last four type declarations are valid -- the most general type is F -- (not E, because Eq is a more general class than Ord) {- Question 4 -} --[2,4,6,8,10,12,14,16,18,20,22,24,26,28,30] x = [2,4..30] x' = [x*2 | x <- [1,2..15]] --[-1,2,-3,4,-5,6,-7,8,-9,10,-11,12,-13,14,-15,16,-17,18,-19,20] y = [(-1)^x*x | x <- [1,2..20]] alternate x acc = if x == 0 then acc else alternate (x-1) (((-1)^x*x):acc) y' = alternate 20 [] {- Question 5 -} -- tail recursively avg xs = avg_helper xs (0,0) avg_helper xs (s,c) = case xs of [] -> if c == 0 then 0 else s/c (x:xs') -> avg_helper xs' (s+x, c+1) sumCount x (s,c) = (s+x,c+1) avg' xs = let (s,c) = foldr sumCount (0,0) xs in if c == 0 then 0 else s / c