[plt-scheme] help writing idiomatic plt scheme

From: Martin DeMello (martindemello at gmail.com)
Date: Sat Jun 14 22:39:07 EDT 2008

I've pasted a program fragment in at http://pastebin.com/f152f0ea4 - I
would like some help improving it in terms of making it more concise
and idiomatic, since I have the feeling I'm doing too much work for a
fundamentally simple problem.


p.s. code below if you don't feel like clicking through to pastebin

; 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)))

Posted on the users mailing list.