Guest User

Untitled

a guest
Sep 24th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.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 :: Integer
  8. ,denominator :: Integer } 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. instance Num Fraction where
  20. lf@(Fraction ln ld) + rf@(Fraction rn rd) = addFractions lf rf
  21. lf@(Fraction ln ld) - rf@(Fraction rn rd) = addFractions lf (negate rf)
  22. lf@(Fraction ln ld) * rf@(Fraction rn rd) = Fraction (ln * rn) (ld * rd)
  23. negate (Fraction ln ld) = Fraction (-ln) ld where
  24. npos = abs ln == ln
  25. dpos = abs ld == ld
  26. abs (Fraction ln ld) = Fraction (abs ln) (abs ld)
  27. signum (Fraction ln ld)
  28. | ln == 0 = 0
  29. | (((ln > 0) && (ld < 0) ) || ((ln < 0) && (ld > 0))) = -1
  30. | otherwise = 1
  31. fromInteger (i) = Fraction (i) (1)
  32. qsort :: (Ord a, Num a) => [a] -> [a]
  33. qsort [] = []
  34. qsort (pivot:rest) = qsort left ++ [pivot] ++ qsort right
  35. where left = filter (<pivot) rest
  36. right = filter (>=pivot) rest
  37.  
  38. --combine :: (Ord a, Num a) => [a] -> [a] -> [a]
  39. --combine lst1 lst2 = let (slst, llst) = if (length lst1 < length lst2) then (lst1, lst2) else (lst2, lst1) in
  40. zip' :: [a] -> [b] -> [(a, b)]
  41. zip' _ [] = []
  42. zip' [] _ = []
  43. zip' a b = (head a, head b) : zip' (tail a) (tail b)
  44.  
  45. zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
  46. zipWith' _ _ [] = []
  47. zipWith' _ [] _ = []
  48. zipWith' fnc (x:xs) (y:ys) = fnc x y : zipWith' fnc xs ys
  49.  
  50. filter' :: (a -> Bool) -> [a] -> [a]
  51. filter' _ [] = []
  52. filter' fnc (x:xs) = body ++ filter' fnc xs where
  53. body = if fnc x then [x] else []
  54.  
  55.  
  56. collatz :: Integer -> [Integer]
  57. collatz x = (takeWhile (/=1) $ iterate nval x) ++ [1] where
  58. nval val
  59. | val == 1 = 1
  60. | even val = val `div` 2
  61. | otherwise = (val * 3) + 1
  62.  
  63.  
  64. maxList :: (Num a, Ord a) => [a] -> a
  65. maxList xa = foldl max 0 xa
  66.  
  67. longestList :: [[a]] -> [a]
  68. longestList = foldl (\la xa -> if (length xa > length la) then xa else la) []
  69.  
  70. -- takes function(acc, curr) default value, list, returns result of acc's type
  71. foldl' :: (a -> b -> a) -> a -> [b] -> a
  72. foldl' fn def [] = def
  73. foldl' fn def (x:xs) = foldl' fn (fn def x) xs
  74.  
  75. flip' :: (a -> b -> c) -> (b -> a -> c)
  76. flip' f = \x y -> f y x
  77.  
  78. multiMap :: [(a -> b)] -> [a] -> [[b]]
  79. multiMap [] _ = []
  80. multiMap (fn:fns) xs = [map fn xs] ++ multiMap fns xs
  81.  
  82. unzip' :: [(a, b)] -> ([a], [b])
  83. unzip' xs = (map fst xs, map snd xs)
  84.  
  85.  
  86. fizzbuzz = map (\x -> if mod x 3 == 0 then if mod x 5 == 0 then "fizzbuzz" else "fizz" else if mod x 5 == 0 then "buzz" else show x) [1..]
Add Comment
Please, Sign In to add comment