Symbolic simplification in Haskell (using recursion?)

后端 未结 2 1069
梦毁少年i
梦毁少年i 2021-01-16 14:32

How can I give a general rule that includes all the expressions below? E.g one expression, another one for sub and one for mult. I need to use recursion but i got confused..

2条回答
  •  不要未来只要你来
    2021-01-16 15:16

    The recursion comes in when you need to deal with nested expressions. For instance, how do you simply (Plus (Plus 2 3) (Plus 4 5))?

    One approach is to break it into two functions. Move the one level logic (which you show above) into its own function. The main simplify function might have a rule similar to the following for Plus:

    simplify (Plus x y) = simplify_one_level (Plus (simplify x) (simplify y))
    

提交回复
热议问题