Files
cs-252/lab04/mapFilter.hs
2026-02-11 11:26:32 -08:00

31 lines
1.0 KiB
Haskell

-- USING MAP, convert a list of Strings to a list of Integers.
stringsToNums :: [String] -> [Integer]
stringsToNums l = map read l
-- USING MAP, take a list of integers and return a list with
-- the equivalent absolute values.
mapAbsVal :: [Integer] -> [Integer]
mapAbsVal l = map abs l
-- USING ZIPWITH, concatenate a list of first names with
-- a list of last names to produce a list of full names.
fullNames :: [String] -> [String] -> [String]
fullNames l = zipWith (\first last -> first ++ " " ++ last) l
-- USING MAP AND FILTER, square all positive numbers in a list;
-- strip out 0 and negative numbers.
squarePositives :: [Integer] -> [Integer]
squarePositives l = map (\x -> x * x) $ filter (\x -> x > 0) l
main :: IO ()
main = do
print $ stringsToNums []
print $ stringsToNums ["1", "2", "3", "4", "5"]
print $ mapAbsVal [-4, 3, 2, -99, 54]
print $ mapAbsVal $ stringsToNums ["-4", "7", "-22"]
print $ fullNames ["John", "Wes Happen", "Holly"] ["Smith", "Ng", "Wood"]
print $ squarePositives [2, -3, 45, 5, 0, 7, -6]