Guest User

Untitled

a guest
Sep 23rd, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.10 KB | None | 0 0
  1. --myApply :: (Num a0) => (a0 -> a0 -> a0) -> [a0] -> a0
  2. --myApply func list
  3. -- = func (head list) value
  4. --where value =
  5.  
  6.  
  7. data Fraction = Fraction { numerator :: Int
  8. ,denominator :: Int } deriving (Eq)
  9.  
  10. addFractions :: Fraction -> Fraction -> Fraction
  11. addFractions (Fraction ln ld) (Fraction rn rd)
  12. | rd == ld = Fraction (ln + rn) ld
  13. | otherwise = Fraction ((ln*rd) + (rn*ld)) newd where
  14. newd = ld*rd
  15.  
  16. instance Show Fraction where
  17. show (Fraction num denom) = (show num) ++ " / " ++ (show denom)
  18.  
  19. qsort :: (Ord a, Num a) => [a] -> [a]
  20. qsort [] = []
  21. qsort (pivot:rest) = qsort left ++ [pivot] ++ qsort right
  22. where left = filter (<pivot) rest
  23. right = filter (>=pivot) rest
  24.  
  25. --combine :: (Ord a, Num a) => [a] -> [a] -> [a]
  26. --combine lst1 lst2 = let (slst, llst) = if (length lst1 < length lst2) then (lst1, lst2) else (lst2, lst1) in
  27. zip' :: [a] -> [b] -> [(a, b)]
  28. zip' _ [] = []
  29. zip' [] _ = []
  30. zip' a b = (head a, head b) : zip' (tail a) (tail b)
  31.  
  32. zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
  33. zipWith' _ _ [] = []
  34. zipWith' _ [] _ = []
  35. zipWith' fnc (x:xs) (y:ys) = fnc x y : zipWith' fnc xs ys
  36.  
  37. filter' :: (a -> Bool) -> [a] -> [a]
  38. filter' _ [] = []
  39. filter' fnc (x:xs) = body ++ filter' fnc xs where
  40. body = if fnc x then [x] else []
  41.  
  42.  
  43. collatz :: Integer -> [Integer]
  44. collatz x = (takeWhile (/=1) $ iterate nval x) ++ [1] where
  45. nval val
  46. | val == 1 = 1
  47. | even val = val `div` 2
  48. | otherwise = (val * 3) + 1
  49.  
  50.  
  51. maxList :: (Num a, Ord a) => [a] -> a
  52. maxList xa = foldl max 0 xa
  53.  
  54. longestList :: [[a]] -> [a]
  55. longestList = foldl (\la xa -> if (length xa > length la) then xa else la) []
  56.  
  57. -- takes function(acc, curr) default value, list, returns result of acc's type
  58. foldl' :: (a -> b -> a) -> a -> [b] -> a
  59. foldl' fn def [] = def
  60. foldl' fn def (x:xs) = foldl' fn (fn def x) xs
  61.  
  62. flip' :: (a -> b -> c) -> (b -> a -> c)
  63. flip' f = \x y -> f y x
Add Comment
Please, Sign In to add comment