如果您来自 OOP 世界,那么将模块视为类似于静态类可能会有所帮助。与.NET静态类类似,OCaml模块也有构造函数;与 .NET 不同,OCaml 模块可以在其构造函数中接受参数。对于传递给模块构造函数的对象来说,函子是一个听起来很吓人的名字。
因此,使用二叉树的规范示例,我们通常会在 F# 中这样编写:
type 'a tree =
| Nil
| Node of 'a tree * 'a * 'a tree
module Tree =
let rec insert v = function
| Nil -> Node(Nil, v, Nil)
| Node(l, x, r) ->
if v < x then Node(insert v l, x, r)
elif v > x then Node(l, x, insert v r)
else Node(l, x, r)
很好,花花公子。但是 F# 如何知道如何比较两个类型的对象'a
使用<
and >
运营商?
在幕后,它做了这样的事情:
> let gt x y = x > y;;
val gt : 'a -> 'a -> bool when 'a : comparison
好吧,如果你有一个类型的对象怎么办Person
哪个不实现该特定接口?如果您想动态定义排序函数怎么办?一种方法是仅传递比较器,如下所示:
let rec insert comparer v = function
| Nil -> Node(Nil, v, Nil)
| Node(l, x, r) ->
if comparer v x = 1 then Node(insert v l, x, r)
elif comparer v x = -1 then Node(l, x, insert v r)
else Node(l, x, r)
It works,但是如果您正在编写一个用于插入、查找、删除等树操作的模块,则需要客户端在每次调用任何内容时传递一个排序函数。
如果 F# 支持函子,其假设语法可能如下所示:
type 'a Comparer =
abstract Gt : 'a -> 'a -> bool
abstract Lt : 'a -> 'a -> bool
abstract Eq : 'a -> 'a -> bool
module Tree (comparer : 'a Comparer) =
let rec insert v = function
| Nil -> Node(Nil, v, Nil)
| Node(l, x, r) ->
if comparer.Lt v x then Node(insert v l, x, r)
elif comparer.Gt v x then Node(l, x, insert v r)
else Node(l, x, r)
仍然使用假设的语法,您可以这样创建模块:
module PersonTree = Tree (new Comparer<Person>
{
member this.Lt x y = x.LastName < y.LastName
member this.Gt x y = x.LastName > y.LastName
member this.Eq x y = x.LastName = y.LastName
})
let people = PersonTree.insert 1 Nil
不幸的是,F# 不支持函子,因此您必须求助于一些混乱的解决方法。对于上面的场景,我几乎总是将“函子”与一些辅助辅助函数一起存储在我的数据结构中,以确保它被正确复制:
type 'a Tree =
| Nil of 'a -> 'a -> int
| Node of 'a -> 'a -> int * 'a tree * 'a * 'a tree
module Tree =
let comparer = function
| Nil(f) -> f
| Node(f, _, _, _) -> f
let empty f = Nil(f)
let make (l, x, r) =
let f = comparer l
Node(f, l, x, r)
let rec insert v = function
| Nil(_) -> make(Nil, v, Nil)
| Node(f, l, x, r) ->
if f v x = -1 then make(insert v l, x, r)
elif f v x = 1 then make(l, x, insert v r)
else make(l, x, r)
let people = Tree.empty (function x y -> x.LastName.CompareTo(y.LastName))