Haskell 中的代数数据类型是否等于 F# 中的可区分联合?
我正在学习 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
(我来自 OCaml,但我查看了相关的 F# 内容,看起来是一样的。如果我错了,请纠正我。)它们是相同的,只是同一事物的不同术语,但有一些语法差异。例如,要定义具有多个数据元素的构造函数,在 OCaml 和 F# 中,您可以像将它们填充在元组中一样编写类型:
Haskell:
OCaml / F#:
类似地,要对其进行模式匹配,您同样可以像单个参数一样操作这是一个包含所有数据成员的元组:
Haskell:
OCaml / F#:
Edit: 显然以下内容不适用于 F#
此外,在 Haskell 中,构造函数自动成为一个可以使用的函数本身就像任何其他值一样: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:
OCaml / F#:
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:
OCaml / F#:
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:OCaml/F# don't do this. You could manually define your own functions for each constructor.我对 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.