;;; Procedure: ;;; preprocess ;;; Parameters: ;;; fname, a string ;;; Purpose: ;;; Read data from the given file, process it into usable form, and return ;;; it. ;;; Produces: ;;; data-points, a list of vectors ;;; Preconditions: ;;; fname names a valid file. ;;; Each line of fname has the form ;;; head1 head2 val1 ... valn ;;; in which the columns are separated by tabs (define preprocess (lambda (fname) (let* ((port (open-input-file fname)) (lines (data-lines (open-input-file fname)))) (close-input-port port) (map list->vector (transpose lines))))) ;;; Procedure: ;;; data-lines ;;: Parameters ;;; port, an open input port ;;; Purpose: ;;; Reads all of the lines from the given port and returns them ;;; in a segmented form. ;;; Produces: ;;; lines, a list of lists of strings. ;;; Preconditions: ;;; port is open for reading. ;;; Each line in the file associated with port has the form ;;; head1 head2 val1 ... valn ;;; in which the columns are separated by tabs and ;;; end with a tab. (define data-lines (lambda (port) (if (eof-object? (peek-char port)) null (cons (map string->number (remove-last (cddr (strbreakup (read-line port) "\t")))) (data-lines port))))) ;;; Procedure: ;;; remove-last ;;; Parameters: ;;; lst, a list ;;; Purpose: ;;; Removes the last element of lst ;;; Produces: ;;; ls, a list ;;; Preconditions: ;;; lst is nonempty. ;;; Postconditions: ;;; (length ls) = 1 + (length lst) ;;; For each i, 0 <= i < (length ls) ;;; (list-ref ls i) = (list-ref lst i) (define remove-last (lambda (lst) (reverse (cdr (reverse lst))))) ;;; Procedure: ;;; transpose ;;; Parameters: ;;; lists, a list of lists ;;; Purpose: ;;; Transpose the lists ;;; Produces: ;;; transposed, a list of lists ;;; Preconditions: ;;; lists has the form ;;; ((x11 x12 x13 ... x1n) ;;; (x21 x22 x23 ... x2n) ;;; (x31 x32 x33 ... x3n) ;;; ... ;;; (xm1 xm2 xm3 ... xmn)) ;;; Postconditions: ;;; transposed has the form ;;; ((x11 x21 x31 ... xm1) ;;; (x12 x22 x32 ... xm2) ;;; (x13 x23 x33 ... xm3) ;;; ... ;;; (x1n x2n x3n ... x3n)) (define transpose (lambda (lists) (if (null? (car lists)) null (cons (map car lists) (transpose (map cdr lists))))))