Haskell 中的代数数据类型是否等于 F# 中的可区分联合?

发布于 2024-12-16 11:28:10 字数 148 浏览 2 评论 0原文

我正在学习 Haskell,想知道 Haskell 中称为代数数据类型的构造是否与 F# 中的可区分联合相同,或者它们之间存在一些细微的差异。

我还希望对 F#(我的第一种函数式语言)和其他函数式语言进行良好的比较,特别是在相似的概念但又具有实质性但重要的差异方面。

I am learning Haskell and would like to know whether the constructs known in Haskell as algebraic datatypes are the same that discriminated unions in F# or there are some subtle differences between them.

I would also appreciate much a good comparison between F# (my first functional language) and other functional languages, especially as regards similar concepts but with substantial but important differences.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

年少掌心 2024-12-23 11:28:10

(我来自 OCaml,但我查看了相关的 F# 内容,看起来是一样的。如果我错了,请纠正我。)它们是相同的,只是同一事物的不同术语,但有一些语法差异。例如,要定义具有多个数据元素的构造函数,在 OCaml 和 F# 中,您可以像将它们填充在元组中一样编写类型:

Haskell:

data Whatever = Foo TypeA TypeB

OCaml / F#:

type whatever = Foo of typeA * typeB

类似地,要对其进行模式匹配,您同样可以像单个参数一样操作这是一个包含所有数据成员的元组:

Haskell:

case x of Foo a b -> ...

OCaml / F#:

match x with Foo (a, b) -> ...

Edit: 显然以下内容不适用于 F#

此外,在 Haskell 中,构造函数自动成为一个可以使用的函数本身就像任何其他值一样:

zipWith Foo xs ys

OCaml/F# 不这样做。您可以为每个构造函数手动定义自己的函数。

(I come from OCaml, but I looked over the relevant F# stuff and it seems the same. Correct me if I'm wrong.) They are the same, just different terminology for the same thing, but there are a few syntactical differences. For example, to define a constructor with multiple data elements, in OCaml and F# you write the type as if they were stuffed in a tuple:

Haskell:

data Whatever = Foo TypeA TypeB

OCaml / F#:

type whatever = Foo of typeA * typeB

Similarly, to pattern match on it, you similarly act like a single argument that is a tuple with all the data members stuffed inside:

Haskell:

case x of Foo a b -> ...

OCaml / F#:

match x with Foo (a, b) -> ...

Edit: apparently the following does not apply in F#

Also, in Haskell the constructor automatically becomes a function that you can use by itself like any other value:

zipWith Foo xs ys

OCaml/F# don't do this. You could manually define your own functions for each constructor.

|煩躁 2024-12-23 11:28:10

我对 Haskell 不太熟悉(我只读过 学习 Haskell)但我还没有发现 DU 和 Haskell 的代数数据类型之间的基本区别——它们都试图对相同的概念进行建模。话虽如此,F# 和 Haskell 具有非常不同的类型系统(例如,Haskell 具有类型类/更高种类的类型;F# 深深扎根于 OOP 等),因此存在不对称性,但不限于这些数据类型。

I'm not very familiar with Haskell (I've only read Learn You a Haskell) but I haven't yet come across a basic difference between DUs and Haskell's algebraic data types--they're both attempts to model the same concept. Having said that, F# and Haskell have very different type systems (e.g., Haskell has type classes/higher-kinded types; F# is deeply grounded in OOP, etc.) so there is asymmetry, but nothing limited to these data types.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文