Haskell's algebraic data types

后端 未结 8 2206
我寻月下人不归
我寻月下人不归 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 06:03

    A simple reason why they are called algebraic; there are both sum (logical disjunction) and product (logical conjunction) types. A sum type is a discriminated union, e.g:

    data Bool = False | True
    

    A product type is a type with multiple parameters:

    data Pair a b = Pair a b
    

    In O'Caml "product" is made more explicit:

    type 'a 'b pair = Pair of 'a * 'b
    

提交回复
热议问题