Cyclotomic polynomial: Difference between revisions

→‎{{header|Haskell}}: fixed pretty printing
(→‎{{header|Haskell}}: added solution)
(→‎{{header|Haskell}}: fixed pretty printing)
Line 1,896:
The task solution
 
<lang haskell>showPoly p[] = foldl showMono "0" $ zip (reverse p) [0..]
showPoly p = foldl1 (\r -> (r ++) . term) $
dropWhile null $
foldMap (\(c, n) -> [show c ++ expt n]) $
zip (reverse p) [0..]
where
showMono r (c, i)expt = r ++ \case (c,0 i)-> of""
(0, _) 1 -> "*x"
(c, 0) n -> "*x^" ++ show cn
 
(1, i) -> " + " ++ "x^" ++ show i
term = (-1,\case i)[] -> " - " ++ "x^" ++ show i
(c, i) | c < 0 -> " - " ++ show'0':'*':t (-c) ++> "*x^" ++ show i
(c, i) | c > 0 -> " + " ++ show'-':'1':'*':t c-> ++" - "*x^" ++ show i t
'1':'*':t -> " + " ++ t
'-':t -> " - " ++ t
(1, i) t -> " + " ++ "x^"t ++ show i
 
main = do
Anonymous user