Advertisement
elvecent

Lab2

Nov 3rd, 2016
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --ghc 8.0.1 /opt/ghc/8.0.1/lib/ghc-8.0.0.20160127/
  2.  
  3. import Data.List
  4. import Data.Maybe
  5.  
  6. newtype Info = Info [Double] deriving (Eq, Show)
  7.  
  8. instance Ord Info where
  9.     compare (Info (a:[b])) (Info (c:[d])) = case compare a c of
  10.                                                 EQ -> compare b d
  11.                                                 cmp -> cmp
  12.  
  13. main = do
  14.         print $ myZip ([1,2,3],[99,98,97,96,95])
  15.         print $ sumSquares [1,2,3]
  16.         print $ sumSquares' [1,2,3]
  17.        print $ sumSquares'' [1,2,3]
  18.        print $ strangeCompare 35 0
  19.        print $ maxString ["BIGGER","NEBEER"]
  20.        print $ maxPoint [(1,1),(1,2),(2,1)]
  21.  
  22. myZip :: ([a],[b]) -> [(a,b)]
  23. myZip ([],_) = []
  24. myZip (_,[]) = []
  25. myZip ((x:xs),(y:ys)) = (x,y) : (myZip (xs,ys))
  26.  
  27. sumSquares :: (Num a) => [a] -> a
  28. sumSquares [] = 0
  29. sumSquares (x:xs) = x*x + (sumSquares xs)
  30.  
  31. sumSquares' :: (Num a) => [a] -> a
  32. sumSquares' = \xs -> iter xs 0
  33.             where iter [] acc = acc
  34.                   iter (x:xs) acc = iter xs (x*x + acc)
  35.                  
  36. --foldl :: (b -> a -> b) -> b -> [a] -> b                  
  37.                  
  38. sumSquares'' :: (Num a) => [a] -> a
  39. sumSquares'' = foldl (\acc x -> x*x + acc) 0
  40.  
  41. sumSquares''' = sum .map (^2)
  42.  
  43.  
  44. sqr' = (^2) . fromIntegral
  45. sumS = sum. map ((1/) . (^2) . fromIntegral)
  46.  
  47. sqr :: Integer -> Double
  48. sqr x = y*y
  49.        where y = fromIntegral x
  50.  
  51. squareHarmSum :: Integer -> Double
  52. squareHarmSum n = foldl (\acc x -> acc + (1/(sqr x))) 0 [1..n]
  53.  
  54. intToLst 0 = []
  55. intToLst x = x `mod` 10 : intToLst (x `div` 10)
  56.                          
  57. lstToInt [] = 0
  58. lstToInt (x:xs) = (lstToInt xs) * 10 + x
  59.  
  60. strangeCompare :: Integer -> Integer -> Ordering
  61. strangeCompare a b = compare (f a) (f b)
  62.                     where f = lstToInt . oddsOut . intToLst
  63.                          
  64.                           oddsOut = filter even
  65.                          
  66. maxString :: [String] -> String
  67. maxString ss = foldl (\acc s -> zipWith max acc s) (ss !! 0) ss
  68.  
  69.  
  70. maxPoint :: [(Double,Double)] -> (Double,Double)
  71. maxPoint xs = xs !! indx
  72.              where info = map extract xs
  73.                    
  74.                    extract (a,b) = ((atan2 b a), len a b, (a,b))
  75.                    
  76.                    len a b = sqrt $ a*a + b*b
  77.                    
  78.                    maxInfo = maximum info
  79.                    
  80.                    indx = fromJust $ elemIndex maxInfo info
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement