Parsing Terms in Brackets for Calculator

Hello, I'm currently doing my first steps in Haskell with a calculator and I'm stuck at the parser. I have a data Term which will build ... basically a tree of operations, and works fine. I need help for the function termify :: [Either Operator Term] -> Term It takes operators (such as +,**) and terms and output a new, bigger term and is mostly trivial. However, all attempts I've done for parsing brackets seem very ... crude and not like Haskell at all. The very first pattern match should check for the innermost brackets, and return termify for everything in between. I guess that I'm missing some really cool, haskelly solution here. Best Regards Leonhard

You're in luck! Text.Megaparsec.Expr[1] is designed to handle exactly this problem. I put a tutorial of sorts[2] in a fork of it on Github. [1] https://www.stackage.org/haddock/nightly-2015-12-08/megaparsec-4.2.0/Text-Me... [2] https://github.com/JeffreyBenjaminBrown/megaparsec/tree/master/Expr-studies On Sat, Feb 2, 2019 at 3:03 AM Leonhard Applis < Leonhard.Applis@protonmail.com> wrote:
Hello,
I'm currently doing my first steps in Haskell with a calculator and I'm stuck at the parser. I have a *data Term* which will build ... basically a tree of operations, and works fine.
I need help for the function termify :: [Either Operator Term] -> Term
It takes operators (such as +,**) and terms and output a new, bigger term and is mostly trivial. However, all attempts I've done for parsing brackets seem very ... crude and not like Haskell at all.
The very first pattern match should check for the innermost brackets, and return termify for everything in between. I guess that I'm missing some really cool, haskelly solution here.
Best Regards Leonhard _______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
-- Jeff Brown | Jeffrey Benjamin Brown Website https://msu.edu/~brown202/ | Facebook https://www.facebook.com/mejeff.younotjeff | LinkedIn https://www.linkedin.com/in/jeffreybenjaminbrown(spammy, so I often miss messages here) | Github https://github.com/jeffreybenjaminbrown
participants (2)
-
Jeffrey Brown
-
Leonhard Applis