Fai*_*bid 3 ocaml functional-programming mutable
我在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};;
Run Code Online (Sandbox Code Playgroud)
当我在ocaml顶级做这个
let x = make _ vec _store;;
Run Code Online (Sandbox Code Playgroud)
然后尝试做x.size
我得到这个错误
Error: This expression has type unit -> vec_store
but an expression was expected of type vec_store
Run Code Online (Sandbox Code Playgroud)
什么似乎是问题?我不明白为什么这不起作用.
谢谢,费萨尔
Chu*_*uck 12
make_vec_store
是一个功能.当你说let x = make_vec_store
,你将x设置为该函数时,就像你写的一样let x = 1
,那就是x会使数字为1.你想要的是调用该函数的结果.根据make_vec_store
定义,它需要()
(也称为"单位")作为参数,所以你会写let x = make_vec_store ()
.