Polynomial synthetic division: Difference between revisions

→‎{{header|Haskell}}: changed to less cryptic non-monadic solution
(→‎{{header|Haskell}}: added solution)
(→‎{{header|Haskell}}: changed to less cryptic non-monadic solution)
Line 348:
 
=={{header|Haskell}}==
<lang haskell>import ControlData.MonadList
 
normalized :: (Eq a, Num a) => [a] -> [a]
Line 359:
shortDiv p1 p2
| isZero p2 = error "zero divisor"
| otherwise = foldM step p1 [1 .. length p1 - length as]
let go 0 p = p
step go i (h:t) = return ([h/a],) : go (i-1) (zipWith (+) (map ((h/a) *) ker) t)
in splitAt k $ go k p1
where
k = length p1 - length as
a:as = normalized p2
step (h:t) = return ([h/a], zipWith (+) (map ((h/a) *) ker) t)
ker = negate <$> (as ++ repeat 0)</lang>
 
Line 380 ⟶ 383:
| isZero p2 = error "zero divisor"
| not (isMonic p2) = error "divisor is not monic"
| otherwise = foldM step p1 [1 .. length p1 - length as]
let go 0 p = p
step go i (h:t) = returnh : go ([h],i-1) (zipWith (+) (map (h *) ker) t)
in splitAt k $ go k p1
where
a:ask = normalizedlength p2p1 - length as
_:as = normalized p2
step (h:t) = return ([h], zipWith (+) (map (h *) ker) t)
ker = negate <$> as ++ repeat 0</lang>
 
Anonymous user