Ocaml:打印出 int 列表数组中的元素 [英] Ocaml: printing out elements in an array of int lists

查看:39
本文介绍了Ocaml:打印出 int 列表数组中的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建状态的函数.状态定义为:

I have a function that creates a state. A state is defined as:

type state = graph * bool array;;

图形是:

type graph = int list array;;

图形是一个数组,在每个索引处可能有一个存储在该索引处的 int 列表.

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.

我定义的函数如下:

let state_of_graph (s:state) (out:out_channel) : unit = 
     match s with
     (g, b)

我基本上想遍历我的图 (g) 并打印出索引以及 int 列表中的每个元素(如果元素存在,否则不要打印出空元素).

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).

我想以这种方式打印出来:(考虑到索引 0 中有 2 个元素)

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?

推荐答案

您对数据结构的描述相当糟糕.例如,你写一个图是一个数组,在每个索引处可能有一个存储在该索引处的 int 列表."嗯,这几乎就是 type graph = int list array 的意思(实际上,每个索引处总是存储一个 int 列表),所以你的英语句子没有传达任何额外的信息.解释每个数组元素代表什么会更有用.既然你在谈论图形,我想它就像 a.(i) 包含 j 意味着从 i 到 <代码>j?

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 like a.(i) contains j means that there's an edge from i to j?

但是,由于您打算执行的任务是根据数据结构描述的,我想我明白您想要做什么.

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.

要遍历数组,您有两种主要可能性:编写 for 循环,或使用 Array.iterArray.iteri 函数之一.此任务似乎非常适合 Array.iteri,因为您只是在遍历数组并且需要知道索引.

To iterate over an array, you have two main possibilities: write a for loop, or use one of the functions Array.iter or Array.iteri. This task seems well suited for Array.iteri since you're just walking over the array and you need to know the index.

let print_graph (g, a : state) (out : out_channel) : unit =
  Array.iteri (fun i l -> …) g;;

好的,现在我们需要知道如何处理每个数组元素.每个元素都是一个整数列表:l 的类型为 int list.从您的示例输出中,您只想按顺序输出列表元素,中间有一个空格.要按顺序遍历列表的元素,标准库函数List.iter 就恰到好处.

Ok, now we need to know what to do about each array element. Each element is a list of integers: l has the type int 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 function List.iter is just right.

let print_node (l : int list) (out : out_channel) : unit =
  List.iter (fun j -> print_char ' '; print_int j) l;;

您现在应该可以使用 print_node 来完成 print_graph.您仍然需要在每行的开头打印索引和箭头,并且只有在有索引时才打印一行.

You should now be able to use print_node to finish print_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:打印出 int 列表数组中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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