Algebraic Data Types
Using algebraic data types we are saying some datatype can be one of the many things, distinguished by and identified by what is called a constructor.
For example, Maybe a
is saying that If something has type Maybe a, it can either be a value which has an a
type and wrapped by a constructor Just
or an empty value Nothing
data Maybe a = Just a
| Nothing
Another example is List
, from its definition we can see that it has a recursive structure. So we can have whatever number of elements in our list, but they have to have the same type.
data List a = Cons a (List a)
| Empty
当前内容版权归 hamler-lang 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 hamler-lang .