Advertisement
Magnect

Untitled

Dec 13th, 2023
1,690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. mySum :: [Int] -> Int
  2. mySum [] = 0
  3. mySum (a:as) =  a + mySum as
  4.  
  5. myProduct :: [Int] -> Int
  6. myProduct [] = 1
  7. myProduct (a:as) =  a * myProduct as
  8.  
  9. myPow :: Int -> Int -> Int
  10. myPow b e
  11.     | e == 0 = 1
  12.     | e == 1 = b
  13.     | otherwise = b * myPow b (e - 1)
  14.  
  15. size :: [Int] -> Int
  16. size [] = 0
  17. size (a:as) = 1 + size as
  18.  
  19. nesimo :: Int -> [Int] -> Int
  20. nesimo n (a:as)
  21.     | n == 0 = a
  22.     | otherwise = nesimo (n - 1) as
  23.  
  24. inverter :: [Int] -> [Int]
  25. inverter [] = []
  26. inverter (a:as) = inverter as ++ (a:[])
  27.  
  28. main :: IO ()
  29. main = do
  30.     putStrLn $ "Soma dos elementos da lista: " ++ show (mySum [1, 2, 3, 4, 5])
  31.     putStrLn $ "Produto dos elementos da lista: " ++ show (myProduct [1, 2, 3, 4, 5])
  32.     putStrLn $ "Tamanho da lista: " ++ show (size [1, 2, 3, 4, 5])
  33.     putStrLn $ "Potenciacao b ^ e: " ++ show (myPow 2 1)
  34.     putStrLn $ "Nesimo elemento da lista (2): " ++ show (nesimo 2 [3, 4, 7, 9, 12])
  35.     putStrLn $ "Inverter lista: " ++ show (inverter [1, 2, 3, 4, 5])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement