Anonymous user
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
normalized :: (Eq a, Num a) => [a] -> [a]
Line 359:
shortDiv p1 p2
| isZero p2 = error "zero divisor"
| otherwise =
let go 0 p = p
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 =
let go 0 p = p
in splitAt k $ go k p1
where
_:as = normalized p2
▲ step (h:t) = return ([h], zipWith (+) (map (h *) ker) t)
ker = negate <$> as ++ repeat 0</lang>
|