Haskell 的代数数据类型 [英] Haskell's algebraic data types

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

问题描述

我正在努力完全理解 Haskell 的所有概念.

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

代数数据类型在哪些方面与泛型类型相似,例如在 C# 和 Java 中?它们有何不同?无论如何,它们有什么代数性?

In what ways are algebraic data types similar to generic types, e.g., in C# and Java? And how are they different? What's so algebraic about them anyway?

我熟悉泛代数及其环和域,但我对 Haskell 的类型如何工作只有一个模糊的概念.

I'm familiar with universal algebra and its rings and fields, but I only have a vague idea of how Haskell's types work.

推荐答案

Haskell 中的Algebraic Data Types"支持全参数多态,这是泛型在技术上更正确的名称,作为一个简单的例子列表数据类型:

"Algebraic Data Types" in Haskell support full parametric polymorphism, which is the more technically correct name for generics, as a simple example the list data type:

 data List a = Cons a (List a) | Nil

等价于(尽可能多,忽略非严格评估等)

Is equivalent (as much as is possible, and ignoring non-strict evaluation, etc) to

 class List<a> {
     class Cons : List<a> {
         a head;
         List<a> tail;
     }
     class Nil : List<a> {}
 }

当然,Haskell 的类型系统允许更多……有趣的类型参数使用,但这只是一个简单的例子.关于代数类型"名称,老实说,我从来没有完全确定它们被命名的确切原因,但假设这是由于类型系统的数学基础.我相信原因归结为 ADT 的理论定义是一组构造函数的产物",但是我离开大学已经有几年了,所以我不再记得细节.

Of course Haskell's type system allows more ... interesting use of type parameters but this is just a simple example. With regards to the "Algebraic Type" name, i've honestly never been entirely sure of the exact reason for them being named that, but have assumed that it's due the mathematical underpinnings of the type system. I believe that the reason boils down to the theoretical definition of an ADT being the "product of a set of constructors", however it's been a couple of years since i escaped university so i can no longer remember the specifics.

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

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