This commit is contained in:
Pal Kerecsenyi 2024-05-08 10:18:41 +01:00
parent 1c9c9422e2
commit 3aeae3617e
Signed by: palk
GPG Key ID: 6891661E25394C2C

View File

@ -1,5 +1,6 @@
data Atree = Number Int | Variable String | Plus Atree Atree | Minus Atree | Times Atree Atree
-- (5 - x) * (2y - (x + 7)))
t :: Atree
t =
Times
@ -14,4 +15,8 @@ type Polynomial = [Monomial]
type Monomial = (Int, [String])
polynomial :: Atree -> Polynomial
polynomial = undefined
polynomial (Number n) = [(n, [])]
polynomial (Variable v) = [(1, [v])]
polynomial (Plus e1 e2) = polynomial e1 ++ polynomial e2
polynomial (Minus e) = map (\(c, xs) -> (-c, xs)) (polynomial e)
polynomial (Times e1 e2) = concatMap (\(c1, xs1) -> map (\(c2, xs2) -> (c1 * c2, xs1 ++ xs2)) (polynomial e2)) (polynomial e1)