[Haskell-beginners] Simplify (normalize) symbolic polynom-like expression

Chaddaï Fouché chaddai.fouche at gmail.com
Sun Jun 17 09:37:54 CEST 2012


On Sun, Jun 17, 2012 at 9:36 AM, Chaddaï Fouché
<chaddai.fouche at gmail.com> wrote:
> normalize (Sum xs) = case sort . filter (/= I 0) . map normalize $ xs of
>  [] -> I 0
>  [a] -> a
>  ys -> sumPrefix ys
>    where
>      sumPrefix (I n : I m : ys) = sumPrefix $ I (n+m) : ys
>      sumPrefix ys = ys

Sorry I did this a bit too fast...

> normalize (Sum xs) = case sumPrefix . sort . filter (/= I 0) . map normalize $ xs of
>  [] -> I 0
>  [a] -> a
>  ys -> Sum ys



More information about the Beginners mailing list