
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)

a function that is supposed to add parenthesis to sin/cos expressions. a) Sin 5 + 3 -> Sin(5) + 3 b) Sin Cos (5+3) -> Sin (Cos(5+3)) c) Sin Cos 5 * 3 -> Sin(Cos(5)) * 3
are you looking for textual representation or actual Haskell code which runs? if actual code, these examples may be re-written like this: a) sin 5 + 3 b) sin $ cos $ 5 + 3 c) sin (cos 5) * 3

The spirit of declarative programming lies in the fact that you try to just
tell the compiler what something means. Instead of thinking about keeping
track of the numbers of parentheses, and then applying them afterwards, I
would suggest you create a function to do the same job:
bracketed :: String -> String
bracketed s = "(" ++ s ++ ")"
This would allow you to remove the numeric argument, and also write cleaner
code.
The second thing that you can improve is the pattern matching. You can use
isPrefixOf
http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html#v:isPref...
to
check functions, or use a take inside a case statement to get cleaner
handling of different cases.
case take 3 str of
"sin" -> ...
"cos" -> ...
_ -> ...
This would also let you debug your code more easily, just as you require.
On 4 November 2015 at 14:51, Imants Cekusins
a function that is supposed to add parenthesis to sin/cos expressions. a) Sin 5 + 3 -> Sin(5) + 3 b) Sin Cos (5+3) -> Sin (Cos(5+3)) c) Sin Cos 5 * 3 -> Sin(Cos(5)) * 3
are you looking for textual representation or actual Haskell code which runs? if actual code, these examples may be re-written like this: a) sin 5 + 3 b) sin $ cos $ 5 + 3 c) sin (cos 5) * 3 _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards Sumit Sahrawat

Thanlks!
On Thu, Nov 5, 2015 at 6:16 PM, Sumit Sahrawat, Maths & Computing, IIT
(BHU)
The spirit of declarative programming lies in the fact that you try to just tell the compiler what something means. Instead of thinking about keeping track of the numbers of parentheses, and then applying them afterwards, I would suggest you create a function to do the same job:
bracketed :: String -> String bracketed s = "(" ++ s ++ ")"
This would allow you to remove the numeric argument, and also write cleaner code.
The second thing that you can improve is the pattern matching. You can use isPrefixOf http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-List.html#v:isPref... to check functions, or use a take inside a case statement to get cleaner handling of different cases.
case take 3 str of "sin" -> ... "cos" -> ... _ -> ...
This would also let you debug your code more easily, just as you require.
On 4 November 2015 at 14:51, Imants Cekusins
wrote: a function that is supposed to add parenthesis to sin/cos expressions. a) Sin 5 + 3 -> Sin(5) + 3 b) Sin Cos (5+3) -> Sin (Cos(5+3)) c) Sin Cos 5 * 3 -> Sin(Cos(5)) * 3
are you looking for textual representation or actual Haskell code which runs? if actual code, these examples may be re-written like this: a) sin 5 + 3 b) sin $ cos $ 5 + 3 c) sin (cos 5) * 3 _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Regards
Sumit Sahrawat
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
goforgit .
-
Imants Cekusins
-
Sumit Sahrawat, Maths & Computing, IIT (BHU)