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

查看:168
本文介绍了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.

但是我该怎么做呢?

推荐答案

您对数据结构的描述很差.例如,您编写图形是一个数组,并且在每个索引处可能在该索引处存储一个int列表."好吧,这几乎就是type graph = int list array的含义(实际上,每个索引始终存储有一个int列表),因此您的英语句子中没有传达任何其他信息.解释每个数组元素代表什么将更为有用.既然您在谈论图形,我想它就像a.(i)包含j一样,意味着从ij会有一条边?

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天全站免登陆