Discussion:
I/O and printing lists question
(too old to reply)
c***@gmail.com
2007-10-31 01:02:40 UTC
Permalink
Hello all,
I hope my problem is appropriate for this board,

Ok, I've been coding for a while with sml, but I have never really
needed to use any of the I/O stuff besides printing to the stdOut.
Nevertheless, I have his problem of needing to print large lists
(i.e., a' list list) and the order of the printing matters (I need it
to be tab delimited so I can stick the output into some stat
packages). Anyway, I have a list (the rows) of columns of a' 's.

So, here is the question: Is there a way to convert that list into a
nicer structure for printing to a file? I've been looking at the sml
Basis library last night and this morning, but nothing seems too
promising and google hasn't turned up much. If nothing works, i'll
write an ugly function to print everything a bit at a time, but I was
hoping I wouldn't have to do that.

Any help would be greatly appreciated
Vesa Karvonen
2007-11-07 23:23:16 UTC
Permalink
***@gmail.com wrote:
[...]
Post by c***@gmail.com
Nevertheless, I have his problem of needing to print large lists
(i.e., a' list list) and the order of the printing matters (I need it
to be tab delimited so I can stick the output into some stat
packages). Anyway, I have a list (the rows) of columns of a' 's.
So, here is the question: Is there a way to convert that list into a
nicer structure for printing to a file?
There might be, but your description of the desired output format and
input list is insuffient to give a really good answer. However, based
on the description you gave, I see no immediate reason to convert the
list of lists to another form. As a first approximation, one could
write something like this:

fun printTabDelimitedTable printElem =
app (fn [] => () (* don't print empty lines *)
| x::xs => (printElem x
; app (fn x => (print "\t" ; printElem x)) xs
; print "\n"))

Now,

val () =
printTabDelimitedTable
(print o Int.toString)
[[3, 4, 1],
[5, 9, 2, 6],
[5]]

produces the output:

3 4 1
5 9 2 6
5

-Vesa Karvonen

Loading...