OCaml 中的可变数据
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试 x = make_vec_store()
try x = make_ vec_store()
作为对所提供的优秀答案的跟进。您可以知道您的示例行:
返回一个函数,因为 repl 会告诉您这一点。您可以从输出中看到 x 的类型为
,不带参数unit
并返回类型vec_store
。声明进行对比。
将此与告诉您 x 的类型为 int 且值为 1 的
As a follow up to the excellent answere provided. You can tell that your example line:
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 parametersunit
and returns a typevec_store
.Contrast this to the declaration
which tells you that x is of type int and value 1.
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 saylet x = make_vec_store
, you are setting x to be that function, just like if you'd writtenlet x = 1
, that would make x the number 1. What you want is the result of calling that function. According tomake_vec_store
's definition, it takes()
(also known as "unit") as an argument, so you would writelet x = make_vec_store ()
.