#! /bin/sh #| Hey Emacs, this is -*-scheme-*- code! #$Id$ exec mzscheme -u "$0" ${1+"$@"} |# #lang scheme (require "../prelude.ss") ; (string->string) -> string -> string -> # (define (interact-with f in out) (write-lines out (f (read-lines in)))) ; string -> (string) (define (read-lines in) (with-input-from-file in (λ () (let next-line ([line (read-line)]) (if (eof-object? line) null (cons line (next-line (read-line)))))) #:mode 'text)) ; (string) -> void (define (write-lines out ls) (with-output-to-file out #:mode 'text #:exists 'replace (λ () (for-each (λ (line) (write-string line) (newline)) ls)))) ; (string) -> (string) (define (transpose-lines lines) (let ([heads (apply string-append (map safe-string-first lines))] [tails (map safe-string-rest lines)]) (if (string-empty? heads) null (cons heads (transpose-lines tails))))) (define empty-string "") (define (string-empty? s) (string=? s empty-string)) (define (safe-string-first s) (if (string-empty? s) empty-string (substring s 0 1))) (define (safe-string-rest s) (if (string-empty? s) empty-string (substring s 1))) ; Program entry point (define (main) (define arguments (current-command-line-arguments)) (if (= (vector-length arguments) 2) (let ([in (vector-ref arguments 0)] [out (vector-ref arguments 1)]) (interact-with transpose-lines in out)) (error "interact-with expects exactly 2 arguments : interact-with in-file-path out-file-path\n\tProvided : " arguments))) (main)