daily pastebin goal
31%
SHARE
TWEET

Untitled

a guest Nov 18th, 2017 110 Never
Upgrade to PRO!
ENDING IN00days00hours00mins00secs
  1. module PolynomialList where
  2. import Test.QuickCheck
  3.  
  4. data Poly =
  5.     X
  6.     | Coef Integer
  7.     | Sum Poly Poly
  8.     | Prod Poly Poly
  9.     deriving Show
  10.  
  11. getPolyList :: FilePath -> IO [Integer]
  12. getPolyList x = do {str <- readFile x;
  13.                     return (stringtoInt (lines str));
  14.                     }
  15.  
  16. stringtoInt :: [String] -> [Integer]
  17. stringtoInt a = map(read :: String -> Integer) a
  18.  
  19. polyListValue :: [Integer] -> Integer -> Integer
  20. polyListValue (p:[]) n = p
  21. polyListValue (p:pl) n = (polyListValue pl n) * n + p
  22.  
  23. polyListDegree :: [Integer] -> Integer
  24. polyListDegree pl
  25.     | pl == [] = error "Undefined"
  26.     | otherwise = (toInteger ((length pl) - 1))
  27.  
  28. polyListDeriv :: [Integer] -> [Integer]
  29. polyListDeriv [] = []
  30. polyListDeriv pl = zipWith (*) x [1..(toInteger(length pl))]
  31.     where x = tail pl
  32.  
  33. polyListSum :: [Integer] -> [Integer] -> [Integer]
  34. polyListSum pl [] = pl
  35. polyListSum [] ql = ql
  36. polyListSum pl ql
  37.     | length pl > length ql = zipWith (+) pl ql ++ drop(length ql) pl
  38.     | length pl < length ql = zipWith (+) pl ql ++ drop(length pl) ql
  39.     | otherwise = zipWith (+) pl ql
  40.  
  41. polyListProd :: [Integer] -> [Integer] -> [Integer]
  42. polyListProd (p:pl) [] = []
  43. polyListProd [] (q:ql) = []
  44. polyListProd (p:pl) (q:ql) = polyListSum x y where
  45.     x = (map (p*) (q:ql))
  46.     y = polyListProd pl (0:q:ql)
  47.  
  48. polyListToPoly :: [Integer] -> Poly
  49. polyListToPoly [] = (Coef 0)
  50. polyListToPoly [p] = (Coef p)
  51. polyListToPoly (p:pl) = Sum (Coef p) (Prod X (polyListToPoly pl))
  52.  
  53. polyToPolyList :: Poly -> [Integer]
  54. polyToPolyList (Coef x) = [x]
  55. polyToPolyList X = [0,1]
  56. polyToPolyList (Sum pl ql) = polyListSum (polyToPolyList pl) (polyToPolyList ql)
  57. polyToPolyList (Prod pl ql) = polyListProd (polyToPolyList pl) (polyToPolyList ql)
  58.  
  59. ---------------------------------QuickCheck---------------------------------------
  60.  
  61. polyValue :: Poly -> Integer -> Integer --takes data type Poly and Integer and outputs Integer
  62. polyValue X n = n --base case: value of X at n
  63. polyValue (Coef x) _ = x --base case: value of coefficent at anything is value of coefficient
  64. polyValue (Sum x y) n = (polyValue x n) + (polyValue y n) --outputs sum of Poly at n for two Poly inputs
  65. polyValue (Prod x y) n = (polyValue x n) * (polyValue y n) --outputs product of Poly at n for two Poly inputs
  66.  
  67. prop_polyListValue :: [Integer] -> Integer -> Bool
  68. prop_polyListValue [] _ = True
  69. prop_polyListValue pl n
  70.     | plv == pv = True
  71.     | otherwise = False
  72.         where
  73.             plv = polyListValue pl n
  74.             pv = polyValue p n
  75.             p = polyListToPoly pl
  76.  
  77. prop_polyListDegree :: [Integer] -> Bool
  78. prop_polyListDegree [] = True
  79. prop_polyListDegree l
  80.     | p >= 0 = True
  81.     | otherwise = False
  82.         where
  83.             p = polyListDegree l
  84.  
  85. prop_polyListDeriv :: [Integer] -> Bool
  86. prop_polyListDeriv [] = True
  87. prop_polyListDeriv pl
  88.     | plDer == plDer' = True
  89.     | otherwise = False
  90.         where
  91.             plDer = toInteger(length(polyListDeriv pl))
  92.             plDer' = toInteger(length(pl)) - 1
  93.  
  94. prop_polyListSum :: [Integer] -> [Integer] -> Integer -> Bool
  95. prop_polyListSum [] _ _ = True
  96. prop_polyListSum _ [] _ = True
  97. prop_polyListSum pl ql n
  98.     | (polyListValue (polyListSum pl ql) n) == (polyListValue pl n) + (polyListValue ql n) = True
  99.     | otherwise = False
  100.  
  101. prop_polyListProd :: [Integer] -> [Integer] -> Integer -> Bool
  102. prop_polyListProd [] _ _ = True
  103. prop_polyListProd _ [] _ = True
  104. prop_polyListProd pl ql n
  105.     | pld == pld' = True
  106.     | otherwise = False
  107.         where
  108.             pld = (polyListValue (polyListProd pl ql) n)
  109.             pld' = (polyListValue pl n) * (polyListValue ql n)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top