Polynomial derivative: Difference between revisions

m
→‎{{header|Haskell}}: fixed pretty printing
(→‎{{header|Haskell}}: added solution)
m (→‎{{header|Haskell}}: fixed pretty printing)
Line 205:
 
showPoly [] = "0"
showPoly p = foldl1 term(\r $-> dropWhile(r null++) $. foldMap showMonoterm) $ zip [0..] p
dropWhile null $
foldMap (i\(c,c n) -> [show c ++ "*x^"expt ++ shown]) i$
zip p [0..]
where
showMonoexpt = pure . \case (_,0) -> ""
(0,c)1 -> show c"*x"
(1,c)n -> show c"*x^" ++ "*x"show n
 
(i,1) -> "x^" ++ show i
term = \case [] -> ""
(i,c) -> show c ++ "*x^" ++ show i
term r = \case [] '0':'*':t -> r ""
'-':'1':'*':t -> r ++ " - " ++ t
'1':'*':t -> r ++ " + " ++ t
'-':t -> " - " ++ t
(i,1)t -> "x^ + " ++ show it
 
main = mapM_ (putStrLn . line) ps
Anonymous user