About
set1 q1toCatYears :: Float -> Float toCatYears n | n <= 0 = abs n 15 | n <= 1 = 15 + n 9 | otherwise = 24 + (n - 2) * 4
main = do let n = -6 let catYears = toCatYears n putStrLn $ "A cat that is " ++ show n ++ " years old in human years is " ++ show catYears ++ " years old in cat years."
q2 reach :: [Int] -> Int reach xs = maximum xs - minimum xs
main = do let n = [3, 7, 2, 9, 4] let r = reach n putStrLn $ show r
set2
q1
divisors :: Int -> Int
divisors n = length [x | x <- [1..n], n mod
x == 0]
main = do
let n = 25
let num_divisors = divisors n
putStrLn $ "The number of divisors of " ++ show n ++ " is " ++ show num_divisors
q2 replaceThird :: [(String, String, String)] -> [Double] -> [(String, String, Double)] replaceThird [] [] = [] replaceThird ((x, y, _):xs) (d:ds) = (x, y, d) : replaceThird xs ds
main = do let lst1 = [("apple", "banana", "carrot"), ("dog", "cat", "fish"), ("red", "green", "blue")] let lst2 = [1.2, 3.4, 5.6] let replaced = replaceThird lst1 lst2 putStrLn $ "Original list: " ++ show lst1 putStrLn $ "Replaced list: " ++ show replaced
set3 q1
runningTotal :: Num a => [a] -> [a] runningTotal xs = scanl1 (+) xs
main = do let nums = [1, 2, 3, 4] let result = runningTotal nums putStrLn $ "Running total of " ++ show nums ++ ": " ++ show result
q2
replaceThird :: [(String, String, String)] -> [Double] -> [(String, String, Double)] replaceThird [] [] = [] replaceThird ((x, y, _):xs) (d:ds) = (x, y, d) : replaceThird xs ds
main = do let lst1 = [("apple", "banana", "carrot"), ("dog", "cat", "fish"), ("red", "green", "blue")] let lst2 = [1.2, 3.4, 5.6] let replaced = replaceThird lst1 lst2 putStrLn $ "Original list: " ++ show lst1 putStrLn $ "Replaced list: " ++ show replaced
SET I
- Write a tunction named toCat Years which takes a floating point number of calendar vears as a parameter, and returns a floating point number of cat vears as its result. It is considered that the first calendar vear of a cat's late is equivalent to 1D human vears, the second calendar year of a cat's life is equivalent to 9 human years, and each subsequent calendar year of a cat's life is equivalent to 4 human years. If the parameter provided to your function is a negative number, n, then your function should return a negative number equal t the number of cat Years that would have been compuled for the absolute value of n Some test cases that vou may want to consider include toCat Years -6.0 should return -63.0 toCat Years -2.5 should return -28.5 toCar Years 0.0 should return 0.0 toCar Years 1.0 should return 15.0 to Car Years 3.0 should return 28.0
- Write a function reach that accepts a list of numbers as a parameter and returns the distance between its least and greatest values
SET 2
- Write a method divisors(int N) that returns the number of divisors of a positive integer N that is passed in to the method. A divisor of an Integer n, also called a factor of n, is an integer which divides n without leaving a remainder. For example, the number 100 has 9 divisors [1, 2, 4, 5, 10 20, 25, 50, 100). Therefore divisors(100) . It is easy to test whether a number is a divisor of another number by the modulus operation. For example again, S is a divisor of 100 because 100 mod S Is equal to 0; 6 is not a divisor of 100 because 100 mod 6 is equal to 4.
- Write a function swapPairs that accepts a list of tuples and returs a new list of pairs with the first and second elements of each pair swapped
SET 3
- Write a procedure named running-total that takes a non-empty list of numbers as an argument and returns another list that contains the running total which is computed as follows:
The first number in the resulting list should be the first number of the argument list, the second number in the resulting list should be the sum of the first and second numbers of the original list and the third number in the resulting list should be the sum of the first. second and third numbers of the original list and so on
For example
(running -total (1 2 3 4)) returns '(1 3 6 10) (running-total *(-5 0 -22 18 55)) returns '(-5-5-27 -9 46)
- Suppose you have two lists. one of (String. String, String) and one of Double. They are of equal size. You want to replace the third string in each tuple with its corresponding Double. Write a function replaceThird that accomplishes this task