-- These are some simple examples of Haskell functions introduced -- in the first lecture of CS 8. Some are taken from SOE. -- Several implement SOE functions in an alternate way, using -- higher-order functions -- by Scot Drysdale, 9/18/07 x :: Double x = 1.234 lst :: [Double] lst = [1.0, 2.0, 3.0, 4.0] -- Computes the area of a circle of a given radius circleArea :: Double -> Double circleArea radius = pi * radius^2 -- Computes the areas of circles whose radii are in a list circleAreas :: [Double] -> [Double] circleAreas radii = map circleArea radii -- Adds 1 to each item in a list incrementList :: [Double] -> [Double] incrementList list = map (+ 1) list -- Triples each item in a list tripleList :: [Double] -> [Double] tripleList list = map (* 3) list -- Scales each item in a list by multiplying it by a scale factor scaleList :: Double -> [Double] -> [Double] scaleList sf list = map (* sf) list -- Alternate way to triple each item in a list tripleList2 :: [Double] -> [Double] tripleList2 list = scaleList 3 list -- Sums the items in a list listSum :: [Double] -> Double listSum list = foldl (+) 0.0 list -- Computes the total area of circles whose radii are in a list totalCircleArea :: [Double] -> Double totalCircleArea radii = listSum (circleAreas radii) -- Computes the total area of circles whose radii are given - -- this time without helper functions totalCircleArea2 :: [Double] -> Double totalCircleArea2 radii = foldl (+) 0.0 (map circleArea radii) -- Counts the number of times that a base appears in a genome countBases :: Char -> [Char] -> Int countBases base genome = length (filter (== base) genome) -- Sum only the positive numbers in a list sumPositives :: [Double] -> Double sumPositives list = listSum (filter (> 0) list) -- Create a merged list where each position contains the larger -- of the numbers in the corresponding postitions in list1 and list2 mergeLargest :: [Double] -> [Double] -> [Double] mergeLargest list1 list2 = zipWith max list1 list2 -- Computes the dot product of two vectors represented as lists dotProd :: [Double] -> [Double] -> Double dotProd a b = foldl (+) 0.0 (zipWith (*) a b)