OCaml 中的可变数据

发布于 2024-08-09 05:01:52 字数 777 浏览 7 评论 0原文

我在 OCaml 中创建了一个可变数据结构,但是当我去访问它时,它给出了一个奇怪的错误,

这是我的代码

type vector = {a:float;b:float};;
type vec_store = {mutable seq:vector array;mutable size:int};;

let max_seq_length = ref 200;;

exception Out_of_bounds;;
exception Vec_store_full;;

let vec_mag {a=c;b=d} = sqrt( c**2.0 +. d**2.0);;


let make_vec_store() = 
    let vecarr = ref ((Array.create (!max_seq_length)) {a=0.0;b=0.0}) in
         {seq= !vecarr;size=0};;

当我在 ocaml 顶级中执行此操作

let x = make _ vec _store;;

然后尝试执行 x.size< /code> 我收到此错误,

Error: This expression has type unit -> vec_store
       but an expression was expected of type vec_store

这似乎是什么问题?我不明白为什么这行不通。

谢谢, 费萨尔

I've created a mutable data structure in OCaml, however when I go to access it, it gives a weird error,

Here is my code

type vector = {a:float;b:float};;
type vec_store = {mutable seq:vector array;mutable size:int};;

let max_seq_length = ref 200;;

exception Out_of_bounds;;
exception Vec_store_full;;

let vec_mag {a=c;b=d} = sqrt( c**2.0 +. d**2.0);;


let make_vec_store() = 
    let vecarr = ref ((Array.create (!max_seq_length)) {a=0.0;b=0.0}) in
         {seq= !vecarr;size=0};;

When I do this in ocaml top-level

let x = make _ vec _store;;

and then try to do x.size I get this error

Error: This expression has type unit -> vec_store
       but an expression was expected of type vec_store

Whats seems to be the problem? I cant see why this would not work.

Thanks,
Faisal

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

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

发布评论

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

评论(3

回眸一笑 2024-08-16 05:01:53

尝试 x = make_vec_store()

try x = make_ vec_store()

提赋 2024-08-16 05:01:53

作为对所提供的优秀答案的跟进。您可以知道您的示例行:

# let x = make_vec_store;;
val x : unit -> vec_store = <fun>

返回一个函数,因为 repl 会告诉您这一点。您可以从输出中看到 x 的类型为 ,不带参数 unit 并返回类型 vec_store

声明进行对比。

# let x = 1;;
val x : int = 1

将此与告诉您 x 的类型为 int 且值为 1 的

As a follow up to the excellent answere provided. You can tell that your example line:

# let x = make_vec_store;;
val x : unit -> vec_store = <fun>

returns a function as the repl will tell you this. You can see from the output that x is of type <fun> that takes no parameters unit and returns a type vec_store.

Contrast this to the declaration

# let x = 1;;
val x : int = 1

which tells you that x is of type int and value 1.

旧竹 2024-08-16 05:01:52

make_vec_store 是一个函数。当您说 let x = make_vec_store 时,您将 x 设置为该函数,就像您编写 let x = 1 一样,这将使 x 成为数字 1您想要是调用该函数的结果。根据 make_vec_store 的定义,它以 () (也称为“unit”)作为参数,因此您可以编写 let x = make_vec_store ()

make_vec_store is a function. When you say let x = make_vec_store, you are setting x to be that function, just like if you'd written let x = 1, that would make x the number 1. What you want is the result of calling that function. According to make_vec_store's definition, it takes () (also known as "unit") as an argument, so you would write let x = make_vec_store ().

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