[Haskell-beginners] Add parenthesis to Sin/Cos expressions

goforgit . teztingit at gmail.com
Wed Nov 4 07:29:24 UTC 2015


Hello!

I've written a function that is supposed to add parenthesis to sin/cos
expressions.

For example

   -  Sin5+3        ->    Sin(5) + 3
   - SinCos(5+3)   ->    Sin(Cos(5+3))
   - SinCos5 * 3   ->    Sin(Cos(5)) * 3

The problem is that it converts the following expression
*SinSin(5+1+2)*
into
*Sin(Sin(5)+1+2)*

Could someone point me to the direction of a solution to this? Thanks in
advance!

convert :: String ->
String
convert s = convert' s
0


convert' :: String -> Int ->
String
convert' [] n                                                         =
replicate n ')'
convert' (a:as) n | ((a == 'n' || a == 's') && ((take 1 as) /= "("))  = a :
"(" ++ (convert' as (n+1))
                 | ((a == '+' || a == '*') && (n > 0))               =
(replicate n ')') ++ [a] ++ (convert' as 0)
                 | otherwise                                         = a :
(convert' as n)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20151104/2babc678/attachment.html>


More information about the Beginners mailing list