初始化数组到空白自定义类型OCAML [英] Initialize Array to Blank custom type OCAML

查看:161
本文介绍了初始化数组到空白自定义类型OCAML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

香港专业教育学院建立了一个自定义数据类型

ive set up a custom data type

type vector = {a:float;b:float};

和我想初始化型矢量,但包含什么,长x只是一个空数组的数组。

and i want to Initialize an array of type vector but containing nothing, just an empty array of length x.

以下

let vecarr = Array.create !max_seq_length {a=0.0;b=0.0}

使得数组初始化为{A = 0,B = 0},并留下了空白给我的错误。是林试图做甚至可能吗?

makes the array init to {a=0;b=0} , and leaving that as blank gives me errors. Is what im trying to do even possible?

推荐答案

您不能在OCaml中一个未初始化数组。但是看看它是这样的:你永远不会在你所造成的未初始化值程序难以重现的bug

You can not have an uninitialized array in OCaml. But look at it this way: you will never have a hard-to-reproduce bug in your program caused by uninitialized values.

如果你最终想要的阵列中放置这些值尚未公布,也许你正在太早创建阵列?考虑使用 Array.init 的确切时刻了必要的输入可用来创建它,而不必提前创建它,离开它暂时未初始化。

If the values you eventually want to place in your array are not available yet, maybe you are creating the array too early? Consider using Array.init to create it at the exact moment the necessary inputs are available, without having to create it earlier and leaving it temporarily uninitialized.

功能 Array.init 在参数采用,它使用来计算每个单元的初始值的函数。

The function Array.init takes in argument a function that it uses to compute the initial value of each cell.

这篇关于初始化数组到空白自定义类型OCAML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆