什么是代数数据类型(ADT)? [英] What is an Algebraic Data Type (ADT)?

查看:240
本文介绍了什么是代数数据类型(ADT)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听过很多人在函数式编程中谈论代数数据类型(不要与抽象数据类型"混淆).我所知道的是,ADT指的是某种复合(通常是递归)数据类型,例如树或数学表达式.

维基百科中,只有这样:

代数数据类型是一种复合类型,即一种类型 由其他类型组合而成.两种常见的代数类型 是产品类型(即元组和记录)和总和类型(即 标记或不相干的并集,或变体类型).

但是没有给出正式定义.

所以我想知道ADT的确切定义是什么?根据Wikipedia,产品类型和总和类型是ADT的两个示例,但是乘积和总和是定义AD​​T的唯一有效操作吗?还有其他允许的操作吗?

解决方案

代数数据类型是复合类型,即由其他类型的组合形成的类型,通常分为两类:总和和乘积.

例如:

货币=美元+欧元+英镑
金钱=金额*货币

读取此内容的方法是按OR转换和,并按AND转换乘积.

产品

产品是一种通常可以使用任何编程语言(无论是否具有功能)创建的类型,例如Kotlin,Java,C#,Swift中的Struct或C#等.

用AND读取组成它们的部分.

金钱=金额*货币

之所以将它们称为乘积,是因为它们可以具有的可能值的数量是组成部件的可能值的数量的乘积.

总和

和类型是代数数据的一种,也称为区分联合或不联合联合,传统上仅在诸如Scala或Haskell之类的语言中具有直接支持.

组成求和类型的部分被读取为OR,因为结果对象的值只能包含一个选项.

货币=美元+欧元+英镑

在这种情况下,货币值只能是USD或EUR或GBP.

之所以称为sum,是因为它们可以具有的可能值的数量是:组成它的部分的可能值的数量之和.

这是我博客的链接(西班牙语),在该博客上,我有一篇关于kotlin示例的更完整文章: http://xurxodev.com/tipos-de-datos-algebraicos/

I have heard people talking a lot about Algebraic Data Types (not to be confused with "Abstract Data Types") in functional programming. All I know is that ADT refers to some kind of composite (often recursive) data types like trees or math expressions.

In Wikipedia, it's only said that:

an algebraic data type is a kind of composite type, i.e., a type formed by combining other types. Two common classes of algebraic types are product types (i.e., tuples and records) and sum types (i.e. tagged or disjoint unions, or variant types).

But no formal definition is given.

So I am wondering what exactly is the definition of ADT? As per Wikipedia, product types and sum types are two examples of ADT, but are product and sum the only valid operations for defining ADT? Are there other operations which are also allowed?

解决方案

The Algebraic Data Types are composite types, that is, a type formed by the combination of other types and are commonly classified in two: sums and products.

For example:

Currency = USD + EUR + GBP
Money = Amount * Currency

The way to read this is to translate sums by OR and products by AND.

Product

A product is a type that can normally be created in any programming language, whether functional or not, such as classes in Kotlin, Java, C #, Struct in Swift or C # etc.

The parts of which they are composed are read with AND.

Money = Amount * Currency

They are known as products because the number of possible values they can have is the product of the number of possible values of the component parts.

Sum

A sum type is a type of algebraic data, also known as discriminated union or disjoint union, which traditionally had only direct support in languages such as Scala or Haskell.

The parts of which a sum type is composed are read as OR because the value of the result object can only contain one of the options.

Currency = USD + EUR + GBP

In this case, the currency value can only be USD or EUR or GBP.

They are known as sum because the number of possible values that can have is: the sum of the number of possible values of the parts that compose it.

This is a link to my blog (spanish), where I have a more complete article with kotlin examples: http://xurxodev.com/tipos-de-datos-algebraicos/

这篇关于什么是代数数据类型(ADT)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆