How to translate logical notation to Haskell syntax

后端 未结 4 1012
天命终不由人
天命终不由人 2021-01-25 13:22

I\'ve recently picked up Haskell at uni and I\'m working my way through a set of exercises, here\'s a snippet of one that I can\'t make sense of:

\"Consider the followin

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-25 14:16

    Years ago, I got clever, and I declared my AST type an instance of Num, Eq and Ord, then defined the mathematical and comparison operators for AST expressions, so that expr1 + expr2 would yield a valid AST. Using sevenj’s declarations, this would be written like (+) x y = Sum x y, where the right-hand side is the constructor of an AST expression. For brevity, one = Lit One and two = Lit Two. Then you might write one + one == two and the operators would generate your AST with the correct precedence. Between that and abuse of the let { ... } in ... syntax to allow for arbitrary indentation, I had a way to write ASTs that was almost just the toy imperative language itself, with some boilerplate above, below and on the left.

    The TA grading my assignment, though, was not amused, and wrote, “This is not Haskell!”

提交回复
热议问题