Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --myApply :: (Num a0) => (a0 -> a0 -> a0) -> [a0] -> a0
- --myApply func list
- -- = func (head list) value
- --where value =
- data Fraction = Fraction { numerator :: Integer
- ,denominator :: Integer } deriving (Eq)
- addFractions :: Fraction -> Fraction -> Fraction
- addFractions (Fraction ln ld) (Fraction rn rd)
- | rd == ld = Fraction (ln + rn) ld
- | otherwise = Fraction ((ln*rd) + (rn*ld)) newd where
- newd = ld*rd
- instance Show Fraction where
- show (Fraction num denom) = (show num) ++ " / " ++ (show denom)
- instance Num Fraction where
- lf@(Fraction ln ld) + rf@(Fraction rn rd) = addFractions lf rf
- lf@(Fraction ln ld) - rf@(Fraction rn rd) = addFractions lf (negate rf)
- lf@(Fraction ln ld) * rf@(Fraction rn rd) = Fraction (ln * rn) (ld * rd)
- negate (Fraction ln ld) = Fraction (-ln) ld where
- npos = abs ln == ln
- dpos = abs ld == ld
- abs (Fraction ln ld) = Fraction (abs ln) (abs ld)
- signum (Fraction ln ld)
- | ln == 0 = 0
- | (((ln > 0) && (ld < 0) ) || ((ln < 0) && (ld > 0))) = -1
- | otherwise = 1
- fromInteger (i) = Fraction (i) (1)
- qsort :: (Ord a, Num a) => [a] -> [a]
- qsort [] = []
- qsort (pivot:rest) = qsort left ++ [pivot] ++ qsort right
- where left = filter (<pivot) rest
- right = filter (>=pivot) rest
- --combine :: (Ord a, Num a) => [a] -> [a] -> [a]
- --combine lst1 lst2 = let (slst, llst) = if (length lst1 < length lst2) then (lst1, lst2) else (lst2, lst1) in
- zip' :: [a] -> [b] -> [(a, b)]
- zip' _ [] = []
- zip' [] _ = []
- zip' a b = (head a, head b) : zip' (tail a) (tail b)
- zipWith' :: (a -> b -> c) -> [a] -> [b] -> [c]
- zipWith' _ _ [] = []
- zipWith' _ [] _ = []
- zipWith' fnc (x:xs) (y:ys) = fnc x y : zipWith' fnc xs ys
- filter' :: (a -> Bool) -> [a] -> [a]
- filter' _ [] = []
- filter' fnc (x:xs) = body ++ filter' fnc xs where
- body = if fnc x then [x] else []
- collatz :: Integer -> [Integer]
- collatz x = (takeWhile (/=1) $ iterate nval x) ++ [1] where
- nval val
- | val == 1 = 1
- | even val = val `div` 2
- | otherwise = (val * 3) + 1
- maxList :: (Num a, Ord a) => [a] -> a
- maxList xa = foldl max 0 xa
- longestList :: [[a]] -> [a]
- longestList = foldl (\la xa -> if (length xa > length la) then xa else la) []
- -- takes function(acc, curr) default value, list, returns result of acc's type
- foldl' :: (a -> b -> a) -> a -> [b] -> a
- foldl' fn def [] = def
- foldl' fn def (x:xs) = foldl' fn (fn def x) xs
- flip' :: (a -> b -> c) -> (b -> a -> c)
- flip' f = \x y -> f y x
- multiMap :: [(a -> b)] -> [a] -> [[b]]
- multiMap [] _ = []
- multiMap (fn:fns) xs = [map fn xs] ++ multiMap fns xs
- unzip' :: [(a, b)] -> ([a], [b])
- unzip' xs = (map fst xs, map snd xs)
- 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