import Data.Complex import Data.List data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show) makePolynominalToTree :: String -> Tree(String) makePolynominalToTree px = let id = elemIndex '+' px id2 = elemIndex '*' px id3 = elemIndex '^' px in case id of Just a -> Node "plus" (makePolynominalToTree (take a px)) (makePolynominalToTree (drop (a+1) px)) Nothing -> case id2 of Just b -> Node "prod" (makePolynominalToTree (take b px)) (makePolynominalToTree (drop (b+1) px)) Nothing -> case id3 of Just c -> Node "pow" (makePolynominalToTree (take c px)) (makePolynominalToTree (drop (c+1) px)) Nothing -> Node px EmptyTree EmptyTree makePtreeToFunction :: Tree(String) -> (Complex Float) -> (Complex Float) makePtreeToFunction (Node "plus" a b) z = (makePtreeToFunction a z) + (makePtreeToFunction b z) makePtreeToFunction (Node "prod" a b) z = (makePtreeToFunction a z) * (makePtreeToFunction b z) makePtreeToFunction (Node "pow" a b) z = (makePtreeToFunction a z)^(round(magnitude(makePtreeToFunction b z) )) makePtreeToFunction (Node x EmptyTree EmptyTree ) z = if x == "z" then z else stringToComplexFloat x pxtoV :: String -> (Complex Float) -> (Complex Float) pxtoV s z = makePtreeToFunction (makePolynominalToTree s) z stringToComplexFloat :: String -> (Complex Float) stringToComplexFloat s = if ':' `elem` s then read s ::(Complex Float) else read (s ++ ":+ 0.0") ::(Complex Float)