About
SET1-1
collatzChainLength :: Int -> Int
collatzChainLength n = if n == 1
then 1
else 1 + collatzChainLength (if even n
then n div
2
else 3 * n + 1)
countCollatzChains :: Int -> Int countCollatzChains n = length [x | x <- [1..n], collatzChainLength x > 15]
main :: IO () main = print (countCollatzChains 100)
SET1-2 myTake :: Int -> [a] -> [a] myTake n xs = if n <= 0 then [] else case xs of [] -> [] (y:ys) -> y : myTake (n-1) ys
myDrop :: Int -> [a] -> [a] myDrop n xs = if n <= 0 then xs else case xs of [] -> [] (_:ys) -> myDrop (n-1) ys
SET2-1
A.
largestMultiple :: Integer -> Integer -> Integer
largestMultiple n divisor = divisor * (n div
divisor)
main = do print(largestMultiple (100000 - 1) 3829) B. oddSquares :: [Integer] oddSquares = [n^2 | n <- [1,3..], n^2 < 10000]
main = do print(sum oddSquares)
SET2-2 elemNested :: (Eq a) => a -> [[a]] -> Bool elemNested _ [] = False elemNested a (x:xs) | elem a x = True | otherwise = elemNested a xs
lengthNested :: [[a]] -> Int lengthNested [] = 0 lengthNested (x:xs) = length x + lengthNested xs
main :: IO () main = do let myList = [[1, 2], [3, 4, 5], [6, 7]] let elemResult = elemNested 3 myList let lengthResult = lengthNested myList putStrLn $ "elemNested 3 myList: " ++ show elemResult putStrLn $ "lengthNested myList: " ++ show lengthResult