Haskell's algebraic data types

后端 未结 8 2185
我寻月下人不归
我寻月下人不归 2020-12-04 05:10

I\'m trying to fully understand all of Haskell\'s concepts.

In what ways are algebraic data types similar to generic types, e.g., in C# and Java? And how are they di

8条回答
  •  自闭症患者
    2020-12-04 05:49

    For me, the concept of Haskell's algebraic data types always looked like polymorphism in OO-languages like C#.

    Look at the example from http://en.wikipedia.org/wiki/Algebraic_data_types:

    data Tree = Empty 
              | Leaf Int 
              | Node Tree Tree
    

    This could be implemented in C# as a TreeNode base class, with a derived Leaf class and a derived TreeNodeWithChildren class, and if you want even a derived EmptyNode class.

    (OK I know, nobody would ever do that, but at least you could do it.)

提交回复
热议问题