In-class-2
Class: CSCE-314
-- Get the total sum of the items in the list
sumList :: [Int] -> Int
-- Note: [Int] means a list of ints
-- This will resemble recurssion
sumList [] = 0
sumList (x : xs) = x + sumList xs
-- x is the first element of the list
-- xs is the rest of the list
-- Usage: ghci> sumList [1,2,3,...]
-- Get the double of the sum of the ints in an array
doubleSum :: [Int] -> Int
doubleSum [] = 0
doubleSum (x : xs) = sumList (x : xs) * 2
-- Get the sum of the even numbers in an int array
sumEvenNumbers :: [Int] -> Int
sumEvenNumbers [] = 0
sumEvenNumbers (x : xs) = sumList (filter (even) (x : xs))
-- Note the use of the filter function and the 'even' keyword
-- filter (> 5) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
-- Output: [6, 7, 8, 9, 10]
-- Get the double of the sum of even numbers in an int array
sumDoubleEvenNumbers :: [Int] -> Int
sumDoubleEvenNumbers [] = 0
sumDoubleEvenNumbers (x : xs) = sumEvenNumbers (x : xs) * 2
main :: IO ()
main = do
let numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print (sumList numbers) -- Output: 55
print (doubleSum numbers) -- Output: 110
print (sumEvenNumbers numbers) -- Output: 30
print (sumDoubleEvenNumbers numbers) -- Output: 60