a guest Jun 14th, 2008 26 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
- ; every line of input-file starts with either "# " or "= ".
- ; (we assume the input is well-formed in that this always holds)
- ; we want to collect the lines into two global vectors, lines-o and lines-p.
- ; runs of "# " or "= " lines should be collected into single strings;
- ; that is, "# " strings and "= " strings should strictly alternate
- (define (append-to-car str lst)
- (cond [(null? lst) (list str)]
- [else (cons (string-append (car lst) "\n" str) (cdr lst))]))
- (define-values (read-o read-p _)
- (for/fold ([o '()] [p '()] [c "# "])
- ([line (in-lines input-file)])
- (match (list (substring line 0 2) (substring line 2))
- [(list "# " rest)
- (cond [(equal? c "# ") (values (append-to-car rest o) p "# ")]
- [else (values (cons rest o) p "# ")])]
- [(list "= " rest)
- (cond [(equal? c "= ") (values o (append-to-car rest p) "= ")]
- [else (values o (cons rest p) "= ")])])))
- (close-input-port input-file)
- (define lines-o (list->vector (reverse read-o)))
- (define lines-p (list->vector (reverse read-p)))
RAW Paste Data