Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --ghc 8.0.1 /opt/ghc/8.0.1/lib/ghc-8.0.0.20160127/
- import Data.List
- import Data.Maybe
- newtype Info = Info [Double] deriving (Eq, Show)
- instance Ord Info where
- compare (Info (a:[b])) (Info (c:[d])) = case compare a c of
- EQ -> compare b d
- cmp -> cmp
- main = do
- print $ myZip ([1,2,3],[99,98,97,96,95])
- print $ sumSquares [1,2,3]
- print $ sumSquares' [1,2,3]
- print $ sumSquares'' [1,2,3]
- print $ strangeCompare 35 0
- print $ maxString ["BIGGER","NEBEER"]
- print $ maxPoint [(1,1),(1,2),(2,1)]
- myZip :: ([a],[b]) -> [(a,b)]
- myZip ([],_) = []
- myZip (_,[]) = []
- myZip ((x:xs),(y:ys)) = (x,y) : (myZip (xs,ys))
- sumSquares :: (Num a) => [a] -> a
- sumSquares [] = 0
- sumSquares (x:xs) = x*x + (sumSquares xs)
- sumSquares' :: (Num a) => [a] -> a
- sumSquares' = \xs -> iter xs 0
- where iter [] acc = acc
- iter (x:xs) acc = iter xs (x*x + acc)
- --foldl :: (b -> a -> b) -> b -> [a] -> b
- sumSquares'' :: (Num a) => [a] -> a
- sumSquares'' = foldl (\acc x -> x*x + acc) 0
- sumSquares''' = sum .map (^2)
- sqr' = (^2) . fromIntegral
- sumS = sum. map ((1/) . (^2) . fromIntegral)
- sqr :: Integer -> Double
- sqr x = y*y
- where y = fromIntegral x
- squareHarmSum :: Integer -> Double
- squareHarmSum n = foldl (\acc x -> acc + (1/(sqr x))) 0 [1..n]
- intToLst 0 = []
- intToLst x = x `mod` 10 : intToLst (x `div` 10)
- lstToInt [] = 0
- lstToInt (x:xs) = (lstToInt xs) * 10 + x
- strangeCompare :: Integer -> Integer -> Ordering
- strangeCompare a b = compare (f a) (f b)
- where f = lstToInt . oddsOut . intToLst
- oddsOut = filter even
- maxString :: [String] -> String
- maxString ss = foldl (\acc s -> zipWith max acc s) (ss !! 0) ss
- maxPoint :: [(Double,Double)] -> (Double,Double)
- maxPoint xs = xs !! indx
- where info = map extract xs
- extract (a,b) = ((atan2 b a), len a b, (a,b))
- len a b = sqrt $ a*a + b*b
- maxInfo = maximum info
- indx = fromJust $ elemIndex maxInfo info
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement