About
toCatYears
toCatYears :: Float -> Float toCatYears calendar_years | calendar_years < 0 = -1 toCatYears (abs calendar_years) | calendar_years <= 1 = calendar_years 15 | calendar_years <= 2 = 15 + (calendar_years - 1) 9 | otherwise = 24 + (calendar_years - 2) 4
main :: IO () main = do print $ toCatYears (-6.0) -- should return -63.0 print $ toCatYears (-2.5) -- should return -28.5 print $ toCatYears 0.0 -- should return 0.0 print $ toCatYears 1.0 -- should return 15.0 print $ toCatYears 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.
reach :: Ord a => [a] -> a reach xs = maximum xs - minimum xs main :: IO () main = do let xs = [1, 2, 3, 4, 5] putStrLn $ "The distance between the minimum and maximum values in " ++ show xs ++ " is " ++ show (reach xs)
--6. Write the Haskell functions for the following --i. Reverse a list
reverseList :: [a] -> [a] reverseList [] = [] reverseList (x:xs) = reverseList xs ++ [x]
--ii. Maximum in the list
maxList :: (Ord a) => [a] -> a maxList [] = error "Empty list" maxList [x] = x maxList (x:xs) = max x (maxList xs)
--iii. Minimum in the list
minList :: (Ord a) => [a] -> a minList [] = error "Empty list" minList [x] = x minList (x:xs) = min x (minList xs)
--iv. Sum of the list
sumList :: (Num a) => [a] -> a sumList [] = 0 sumList (x:xs) = x + sumList xs
--v. Product of the list
productList :: (Num a) => [a] -> a productList [] = 1 productList (x:xs) = x * productList xs
--vi. Repeat the element of the list
repeatList :: a -> [a] repeatList x = x:repeatList x
--vii. Init function of the list
initList :: [a] -> [a] initList [] = [] initList [x] = [] initList (x:xs) = x:initList xs
--main main :: IO () main = do print (reverseList [1,2,3,4,5]) print (maxList [1,2,3,4,5]) print (minList [1,2,3,4,5]) print (sumList [1,2,3,4,5]) print (productList [1,2,3,4,5]) print (take 5 (repeatList 1)) print (initList [1,2,3,4,5])
Write a function( program with main function also) in haskell swapPairs that accepts a list of tuples and returns a new list of pairs with 1st and 2nd element of each pair swappedswapPairs :: [(a, b)] -> [(b, a)]
swapPairs [] = [] swapPairs ((x,y):xs) = (y,x) : swapPairs xs main :: IO () main = do let pairs = [(1, 2), (3, 4), (5, 6)] let swappedPairs = swapPairs pairs print swappedPairs
Write a function( program with main function also) in haskell to calculate the number of divisors
numDivisors :: Int -> Int
numDivisors n = length [x | x <- [1..n], n mod
x == 0]
main :: IO ()
main = do
let n = 12
let num = numDivisors n
putStrLn $ "The number of divisors of " ++ show n ++ " is " ++ show nu