It has given me an infinite loop and out-of-memory exception. I tried different variations but not found the valid one and don't know what to do next.<br><br>
<div class="gmail_quote">2010/9/1 jean verdier <span dir="ltr"><<a href="mailto:verdier.jean@gmail.com">verdier.jean@gmail.com</a>></span><br>
<blockquote style="BORDER-LEFT: #ccc 1px solid; MARGIN: 0px 0px 0px 0.8ex; PADDING-LEFT: 1ex" class="gmail_quote">I think that you need to reduce inner expressions before simplification.<br>
<div class="im"><br>s rdc (Add a b) = rdc a `Add` rdc b<br><br></div>will not reduce the Add result. What you need is something like<br><br>s rdc (Add a b) = rdc (rdc a `Add` rdc b)<br><br>that might not terminate but is your target.<br>
<div>
<div></div>
<div class="h5"><br><br>On Wed, 2010-09-01 at 15:19 +0600, Alexander.Vladislav.Popov wrote:<br>> Hi.<br>><br>> Please, tell me where I'm wrong and how to improve my approach.<br>><br>> I'm trying to simplify algebraic expressions this way:<br>
><br>> import Data.Ratio<br>><br>> data Func = Const (Ratio Int)<br>> | Pow (Ratio Int)<br>> | Add Func Func<br>> | Mul Func Func<br>><br>> instance Show Func where<br>
> show (Const n) = "(" ++ show n ++ ")"<br>> show (Pow n) | n == 0 = "1"<br>> | n == 1 = "x"<br>> | otherwise = "(x**(" ++ show n ++ "))"<br>
> show (Add t1 t2) ="(" ++ (show t1) ++ "+" ++ (show t2) ++ ")"<br>> show (Mul t1 t2) ="(" ++ (show t1) ++ "*" ++ (show t2) ++ ")"<br>><br>><br>
> deriv (Const _) = Const 0<br>> deriv (Pow 1) = Const 1<br>> deriv (Pow n) = Const n `Mul` Pow (n-1)<br>> deriv (Add a b) = deriv a `Add` deriv b<br>> deriv (Mul a b) = Add (deriv a `Mul` b) (a `Mul` deriv b)<br>
><br>> p0 = Const 1<br>> p1 = p0 `Add` (Mul (Pow 1) (Const 2))<br>> p2 = p1 `Add` (Mul (Pow 2) (Const 3))<br>><br>><br>> s rdc (Const x) = Const x<br>> s rdc (Pow 0) = Const 1<br>> s rdc (Pow x) = Pow x<br>
> s rdc (Add (Const a) (Const b)) = Const (a+b)<br>> s rdc (Mul (Const 0) _) = Const 0<br>> s rdc (Mul _ (Const 0)) = Const 0<br>> s rdc (Mul (Const a) (Const b)) = Const (a*b)<br>> s rdc (Mul (Pow n) (Pow m)) = Pow (n+m)<br>
><br>> s rdc (Add x (Const 0)) = rdc x<br>> s rdc (Add (Const 0) x) = rdc x<br>> s rdc (Mul (Const m) (Mul (Const n) x)) = rdc $ Mul (Const (n*m)) (rdc<br>> x)<br>> s rdc (Mul x (Const 1)) = rdc x<br>
> s rdc (Mul x (Const a)) = rdc $ Mul (Const a) (rdc x)<br>> s rdc (Mul (Const 1) x) = rdc x<br>> s rdc (Mul x (Add a b)) = Mul (rdc x) (rdc a) `Add` Mul (rdc x) (rdc<br>> b)<br>> s rdc (Mul (Add a b) x) = Mul (rdc a) (rdc x) `Add` Mul (rdc b) (rdc<br>
> x)<br>> s rdc (Mul a b) = rdc a `Mul` rdc b<br>> s rdc (Add a b) = rdc a `Add` rdc b<br>><br>> fix f = f (fix f)<br>><br>> The result I got is :<br>><br>> *Main> fix s $ deriv p2<br>> (((2 % 1)+(0 % 1))+(((6 % 1)*x)+(0 % 1)))<br>
><br>> instead of the anticipated expression ((2 % 1)+((6 % 1)*x)).<br>> And worst of all, I must apply (fix s) repeatedly to achieve correct<br>> answer:<br>><br>> *Main> fix s $ fix s $ deriv p2<br>
> ((2 % 1)+((6 % 1)*x)).<br>><br>> I'll be very much appriciated for any help and useful links.<br></div></div>> _______________________________________________<br>> Beginners mailing list<br>> <a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br><br><br></blockquote></div><br>