Parsing Terms in Brackets for Calculator

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Parsing Terms in Brackets for Calculator

Leonhard Applis
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Parsing Terms in Brackets for Calculator

Jeffrey Brown
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.


On Sat, Feb 2, 2019 at 3:03 AM Leonhard Applis <[hidden email]> 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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


--
Jeff Brown | Jeffrey Benjamin Brown
Website   |   Facebook   |   LinkedIn(spammy, so I often miss messages here)   |   Github   

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners