Abusing the algebra of algebraic data types - why does this work?

后端 未结 7 1691
失恋的感觉
失恋的感觉 2020-11-28 17:23

The \'algebraic\' expression for algebraic data types looks very suggestive to someone with a background in mathematics. Let me try to explain what I mean.

Having de

7条回答
  •  感动是毒
    2020-11-28 17:38

    It seems that all you're doing is expanding the recurrence relation.

    L = 1 + X • L
    L = 1 + X • (1 + X • (1 + X • (1 + X • ...)))
      = 1 + X + X^2 + X^3 + X^4 ...
    
    T = 1 + X • T^2
    L = 1 + X • (1 + X • (1 + X • (1 + X • ...^2)^2)^2)^2
      = 1 + X + 2 • X^2 + 5 • X^3 + 14 • X^4 + ...
    

    And since the rules for the operations on the types work like the rules for arithmetic operations, you can use algebraic means to help you figure out how to expand the recurrence relation (since it is not obvious).

提交回复
热议问题