Task

Write two equal-sized numerical arrays 'x' and 'y' to
a two-column text file named 'filename'.

The first column of the file contains values from an 'x'-array with a
given 'xprecision', the second -- values from 'y'-array with 'yprecision'.

For example, considering:

   x = {1, 2, 3, 1e11};
   y = {1, 1.4142135623730951, 1.7320508075688772, 316227.76601683791}; 
                                                          /* sqrt(x) */
   xprecision = 3;
   yprecision = 5;

The file should look like:

   1    1
   2    1.4142
   3    1.7321
   1e+011   3.1623e+005

This task is intended as a subtask for Measure relative performance of sorting algorithms implementations.




; file:   write-float-array.lsp
; url:    http://rosettacode.org/wiki/Write_float_arrays_to_a_text_file
; author: oofoe 2012-01-30
 
; The "transpose" function is used to flip the joined lists around so
; that it's easier to iterate through them together. 
 
(define (write-float-array x xp y yp filename)
  (let ((f (format "%%-10.%dg %%-10.%dg" xp yp))
        (o (open filename "write")))
    (dolist (v (transpose (list x y)))
      (write-line o (format f (v 0) (v 1))))
    (close o)
  ))
 
; Test
 
(write-float-array   
 '(1 2 3 1e11) 3
 '(1 1.4142135623730951 1.7320508075688772 316227.76601683791) 5
 "filename.chan")
 
(println "File contents:")
(print (read-file "filename.chan"))
 
(exit)