【问题标题】:Define a matrix module in OCaml在 OCaml 中定义一个矩阵模块
【发布时间】:2012-01-14 17:59:42
【问题描述】:

我想构建一个通用矩阵模块,其中包括诸如长度、宽度、大小、read_element、write_element 等通用操作。因为矩阵可以定义为多种类型:element array arrayelement list listmap,这个模块的好处是可以处理里面的矩阵类型的细节,不用在调用模块的层级上费心了。此刻,我想到了以下几点:

module type A_TYPE =
  sig
    type t
  end;;

module matrix =
  functor (Elt: A_TYPE) 
  struct
    type element = Elt.t
    type t = element array array 
      (* `element list list` if I want a matrix to be a list of a list *)

    let length (m: t) : int =
       Array.length a
      (* `List.length a` if t = element list list *)

    ... ...

  end

所以从外面,可以写:

module MyInt = struct type t = int end
module MatInt = Matrix(MyInt)
module MyFloat = struct type t = float end
module MatFloat = Matrix(MyFloat)

let m = MatInt.make 3 4 0
let n = MatFloat.make 3 4 0.2

我的问题是:

1) 我必须先定义一个模块MyInt,然后将其放入函子中以创建MatInt...我发现它是多余的...有没有办法直接实例化一个矩阵模块,其元素有int 作为类型?

2) 考虑到(* ... *) 中写的内容,有没有办法实现矩阵模块,使其同时处理矩阵类型的不同可能性?也许有模式匹配?

希望我的问题很清楚,有人可以帮忙吗?

【问题讨论】:

  • 我觉得你在重新发明轮子。看看波尔卡pop-art.inrialpes.fr/~bjeannet/newpolka/html/…
  • @pad:你知道这个question吗?谢谢
  • 对不起,我不使用波尔卡。我在这里提到它是因为它的模块签名完全符合您的要求。
  • 其实我刚刚得知 Polka 中不再存在 Matrix 模块...
  • 真的吗?你从哪里得到这些信息?

标签: module matrix ocaml


【解决方案1】:

至于问题 #1:您不必定义 MyInt 模块,您可以通过以下方式省略该步骤:

 module MatInt = Matrix(struct type t = int end)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-31
    • 2017-09-10
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多