Ocaml: printing out elements in an array of int lists
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 boole开发者_StackOverflowan 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?
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.
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;;
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;;
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.
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?
精彩评论