Run Code

Code Wall

Users

Misc

Feedback

About

Login

Theme

Privacy
tried some polynoms 2: now with some `*` and foldr(p)
main :: IO () main = do print $ foldr (\x acc > x * acc) 1 Null print $ foldr (\x acc > x * acc) 1 (1:+:2:+:Null) print $ (1 :+: 2 :+: 3 :+: 4 :+: Null) * (1 :+: 1 :+: Null) infixr 5 :+: data Polynomial a = Null  a :+: Polynomial a deriving (Show, Eq) deg :: Polynomial a > Int deg Null = 1 deg (c :+: cs) = deg cs + 1 terms :: (Num a) => Polynomial a > [Polynomial a] terms Null = [] terms (c :+: cs) = (c :+: foldr (\x acc > 0 :+: acc) Null [0 .. deg cs]) : terms cs times :: (Num a) => a > Polynomial a > Polynomial a times k g = foldr (\x acc > (k * x) :+: acc) Null g infixl 4 <<  I can't define :^: and :<< somewhy (<<) :: (Num a) => Polynomial a > Int > Polynomial a (<<) Null _ = Null (<<) g@(a :+: as) n  n > 0 = foldrp (\x acc > x :+: acc) (foldr (\x acc > 0 :+: acc) Null [1 .. n]) g  otherwise = g foldrp :: (Num a) => (a > b > b) > b > Polynomial a > b foldrp f z Null = f 0 z foldrp f z (c :+: Null) = f c z foldrp f z (c :+: cs) = f c (foldr f z cs) instance Foldable Polynomial where foldr = foldrp isn't working; also idk what this means: foldMap f Null = mempty foldMap f (c :+: Null) = f c foldMap f (c :+: cs) = f c `mappend` foldMap f cs instance (Num a, Eq a) => Num (Polynomial a) where (+) Null g = g (+) h Null = h (+) (0 :+: as) (0 :+: bs) = as + bs (+) h@(a :+: as) g@(b :+: bs)  deg h == deg g = a + b :+: as + bs  deg h > deg g = a :+: as + g  otherwise = b :+: h + bs (*) Null h = Null (*) g Null = Null (*) g@(0 :+: as) h@(b :+: bs) = as * h (*) g@(a :+: as) h@(0 :+: bs) = g * bs (*) g@(a :+: as) h = (a `times` h << deg g) + as * h negate Null = Null negate (c :+: cs) = negate c :+: negate cs abs Null = Null abs (0 :+: cs) = abs cs abs (c :+: cs) = abs c :+: abs cs signum Null = 0 signum _ = undefined fromInteger 0 = Null fromInteger i = fromInteger i :+: Null toInteger Null = 0 toInteger (c :+: Null) = c toInteger g = undefined  data GF2 = Zero  One deriving (Show) instance Num (GF2) where (+) Zero Zero = Zero (+) Zero One = One (+) One Zero = One (+) One One = Zero (*) Zero Zero = Zero (*) Zero One = Zero (*) One Zero = Zero (*) One One = One () Zero Zero = Zero () Zero One = One () One Zero = One () One One = Zero abs = id signum = id fromInteger k  odd k = One  otherwise = Zero data GF n = 0,1..n  deriving (Show, Num)
run

edit

history

help
0
Please
log in
to post a comment.
Split into n distinct parts
Welcome
equiv_strings
Is Preorder BST
Exercise
Anonymous recursion. Why doesnt compile?
Reader
Calculate Integral of function
FizzBuzzkell
equiv
Please log in to post a comment.