Advertisement
Rementai

t11

May 11th, 2023
988
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. average :: [Int] -> Int
  2. average xs = sum xs `div` length xs
  3.  
  4. average' :: [Int] -> Int
  5. average' xs = foldr (+) 0 xs `div` length xs
  6.  
  7.  
  8.  
  9. splaszcz :: [[a]] -> [a]
  10. splaszcz xss = foldl (++) [] xss
  11.  
  12.  
  13.  
  14. multiplyLengthLambda :: [Int] -> [Int]
  15. multiplyLengthLambda xs = map (\x -> x * length xs) xs
  16.  
  17. multiplyLengthPartial :: [Int] -> [Int]
  18. multiplyLengthPartial xs = map (* length xs) xs
  19.  
  20. =-=-=-=
  21.  
  22. factorial :: Int -> Int
  23. factorial n
  24.   | n < 0 = -1
  25.   | n == 0 = 1
  26.   | otherwise = n * factorial (n - 1)
  27.  
  28. factorial :: Int -> Int
  29. factorial n = case n of
  30.   _ | n < 0 -> -1
  31.     | n == 0 -> 1
  32.     | otherwise -> n * factorial (n - 1)
  33.  
  34. =-=-=-=
  35.  
  36. findLastIndex :: Eq a => a -> [a] -> Int
  37. findLastIndex target xs = findLastIndexHelper target xs (-1) 0
  38.   where
  39.     findLastIndexHelper _ [] lastIndex _ = lastIndex
  40.     findLastIndexHelper target (x:xs) lastIndex currentIndex =
  41.       if x == target
  42.         then findLastIndexHelper target xs currentIndex (currentIndex + 1)
  43.         else findLastIndexHelper target xs lastIndex (currentIndex + 1)
  44.  
  45.  
  46. findFirstIndex :: Eq a => a -> [a] -> Int
  47. findFirstIndex target xs = findFirstIndexHelper target xs (-1) 0
  48.   where
  49.     findFirstIndexHelper _ [] lastIndex _ = lastIndex
  50.     findFirstIndexHelper target (x:xs) lastIndex currentIndex
  51.       | x == target = findFirstIndexHelper target xs currentIndex (currentIndex + 1)
  52.       | otherwise = findFirstIndexHelper target xs lastIndex (currentIndex + 1)
  53.  
  54.  
  55.  
  56.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement