Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- module PolynomialList where
- import Test.QuickCheck
- data Poly =
- X
- | Coef Integer
- | Sum Poly Poly
- | Prod Poly Poly
- deriving Show
- getPolyList :: FilePath -> IO [Integer]
- getPolyList x = do {str <- readFile x;
- return (stringtoInt (lines str));
- }
- stringtoInt :: [String] -> [Integer]
- stringtoInt a = map(read :: String -> Integer) a
- polyListValue :: [Integer] -> Integer -> Integer
- polyListValue (p:[]) n = p
- polyListValue (p:pl) n = (polyListValue pl n) * n + p
- polyListDegree :: [Integer] -> Integer
- polyListDegree pl
- | pl == [] = error "Undefined"
- | otherwise = (toInteger ((length pl) - 1))
- polyListDeriv :: [Integer] -> [Integer]
- polyListDeriv [] = []
- polyListDeriv pl = zipWith (*) x [1..(toInteger(length pl))]
- where x = tail pl
- polyListSum :: [Integer] -> [Integer] -> [Integer]
- polyListSum pl [] = pl
- polyListSum [] ql = ql
- polyListSum pl ql
- | length pl > length ql = zipWith (+) pl ql ++ drop(length ql) pl
- | length pl < length ql = zipWith (+) pl ql ++ drop(length pl) ql
- | otherwise = zipWith (+) pl ql
- polyListProd :: [Integer] -> [Integer] -> [Integer]
- polyListProd (p:pl) [] = []
- polyListProd [] (q:ql) = []
- polyListProd (p:pl) (q:ql) = polyListSum x y where
- x = (map (p*) (q:ql))
- y = polyListProd pl (0:q:ql)
- polyListToPoly :: [Integer] -> Poly
- polyListToPoly [] = (Coef 0)
- polyListToPoly [p] = (Coef p)
- polyListToPoly (p:pl) = Sum (Coef p) (Prod X (polyListToPoly pl))
- polyToPolyList :: Poly -> [Integer]
- polyToPolyList (Coef x) = [x]
- polyToPolyList X = [0,1]
- polyToPolyList (Sum pl ql) = polyListSum (polyToPolyList pl) (polyToPolyList ql)
- polyToPolyList (Prod pl ql) = polyListProd (polyToPolyList pl) (polyToPolyList ql)
- ---------------------------------QuickCheck---------------------------------------
- polyValue :: Poly -> Integer -> Integer --takes data type Poly and Integer and outputs Integer
- polyValue X n = n --base case: value of X at n
- polyValue (Coef x) _ = x --base case: value of coefficent at anything is value of coefficient
- polyValue (Sum x y) n = (polyValue x n) + (polyValue y n) --outputs sum of Poly at n for two Poly inputs
- polyValue (Prod x y) n = (polyValue x n) * (polyValue y n) --outputs product of Poly at n for two Poly inputs
- prop_polyListValue :: [Integer] -> Integer -> Bool
- prop_polyListValue [] _ = True
- prop_polyListValue pl n
- | plv == pv = True
- | otherwise = False
- where
- plv = polyListValue pl n
- pv = polyValue p n
- p = polyListToPoly pl
- prop_polyListDegree :: [Integer] -> Bool
- prop_polyListDegree [] = True
- prop_polyListDegree l
- | p >= 0 = True
- | otherwise = False
- where
- p = polyListDegree l
- prop_polyListDeriv :: [Integer] -> Bool
- prop_polyListDeriv [] = True
- prop_polyListDeriv pl
- | plDer == plDer' = True
- | otherwise = False
- where
- plDer = toInteger(length(polyListDeriv pl))
- plDer' = toInteger(length(pl)) - 1
- prop_polyListSum :: [Integer] -> [Integer] -> Integer -> Bool
- prop_polyListSum [] _ _ = True
- prop_polyListSum _ [] _ = True
- prop_polyListSum pl ql n
- | (polyListValue (polyListSum pl ql) n) == (polyListValue pl n) + (polyListValue ql n) = True
- | otherwise = False
- prop_polyListProd :: [Integer] -> [Integer] -> Integer -> Bool
- prop_polyListProd [] _ _ = True
- prop_polyListProd _ [] _ = True
- prop_polyListProd pl ql n
- | pld == pld' = True
- | otherwise = False
- where
- pld = (polyListValue (polyListProd pl ql) n)
- pld' = (polyListValue pl n) * (polyListValue ql n)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement