Ocaml:打印出 int 列表数组中的元素
我有一个创建状态的函数。状态定义为:
type state = graph * bool array;;
图是:
type graph = int list array;;
图是一个数组,并且在每个索引处可能存储有一个 int 列表。
我有一个创建状态的函数,我正在尝试将状态中的元素打印到输出文件中。
我定义该函数如下:
let state_of_graph (s:state) (out:out_channel) : unit =
match s with
(g, b)
我基本上想迭代我的图(g)并打印出索引以及 int 列表中的每个元素(如果元素存在,否则不打印出空元素)。
我想以以下方式打印它们: (考虑到索引 0 有 2 个元素)
index0 -> element1
index 0-> element2
1 -> 2 3
状态本身既是一个图又是一个布尔数组。我对从这里到底要做什么(实施)感到困惑。我知道我必须做什么,即迭代我的图表,并打印出索引,后跟一个箭头,后跟单独的整数。
但我到底该怎么做呢?
I have a function that creates a state. A state is defined as:
type state = graph * bool array;;
A graph is:
type graph = int list array;;
A graph is an array and at each index there might be an int list stored at that index.
I have a function that made a state and I am trying to print out the elements in the state to an output file.
I defined the function as follows:
let state_of_graph (s:state) (out:out_channel) : unit =
match s with
(g, b)
I basically want to iterate through my graph (g) and print out the index and also each element in the int list (if elements exist, otherwise don't print out empty elements).
I want to print them out in the manner:
(Considering index 0 has 2 elements in it)
index0 -> element1
index 0-> element2
1 -> 2 3
The state itself is both a graph and a boolean array. I am confused on what to exactly do from here (the implementation). I know what I have to do, which is iterate through my graph, and print out index followed by an arrow followed by the separate ints.
But how exactly do I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您对数据结构的描述相当糟糕。例如,您可以写“图形是一个数组,每个索引处可能存储一个 int 列表”。好吧,这几乎就是 type graph = int list array 的意思(事实上,每个索引处总是存储一个 int 列表),所以你的英语句子不会传达任何额外的信息。解释每个数组元素代表什么会更有用。既然你在谈论图形,我猜它类似于
a.(i)
containsj
意味着从i
到 <代码>j?但是,由于您要执行的任务是根据数据结构来描述的,因此我想我明白您想要做什么。
要迭代数组,您有两种主要可能性:编写
for
循环,或使用Array.iter
或Array.iteri
函数之一>。此任务似乎非常适合 Array.iteri,因为您只是遍历数组并且需要知道索引。好的,现在我们需要知道如何处理每个数组元素。每个元素都是一个整数列表:
l
的类型为int list
。从示例输出中,您只想按顺序输出列表元素,中间有一个空格。要按顺序迭代列表的元素,标准库函数List.iter
正好合适。您现在应该能够使用
print_node
来完成print_graph
。您仍然需要在每行开头打印索引和箭头,并且仅在有索引时才打印行。Your description of your data structures is fairly poor. For example, you write “A graph is an array and at each index there might be an int list stored at that index.” Well, that's almost what
type graph = int list array
means (in fact, there always is an int list stored at each index), so your English sentence doesn't convey any additional information. It would be more useful to explain what each array element represents. Since you're talking about graph, I guess it's something likea.(i)
containsj
means that there's an edge fromi
toj
?However, since the task you set out to perform is described in terms of the data structure, I think I understand what you want to do.
To iterate over an array, you have two main possibilities: write a
for
loop, or use one of the functionsArray.iter
orArray.iteri
. This task seems well suited forArray.iteri
since you're just walking over the array and you need to know the index.Ok, now we need to know what to do about each array element. Each element is a list of integers:
l
has the typeint list
. From your sample output, you just want to output the list elements in order, with a space in between. To iterate over the elements of a list in order, the standard library functionList.iter
is just right.You should now be able to use
print_node
to finishprint_graph
. You'll still have to print the index and the arrow at the beginning of each line, and to print a line only if there is an index.我忘记了我的大部分 ocaml,很抱歉我没有编写代码:-)
数组和列表都有
map
吗?如果是这样,则映射数组并返回索引 + " -> ",并且在该映射中还附加映射列表时得到的内容,将整数转换为字符串并连接它们。这有帮助吗?
I forgot most of my ocaml, so sorry that I don't write code :-)
Does both array and list have a
map
? if so, map over the array and return index + " -> ", and in that map also append what you get when you map over the list, convert ints to strings and concat them.Was that helpful at all?