Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

Martin DeMello

By: a guest on Jun 14th, 2008  |  syntax: Scheme  |  size: 1.09 KB  |  hits: 25  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. ; every line of input-file starts with either "# " or "= ".
  2. ; (we assume the input is well-formed in that this always holds)
  3. ; we want to collect the lines into two global vectors, lines-o and lines-p.
  4. ; runs of "# " or "= " lines should be collected into single strings;
  5. ; that is, "# " strings and "= " strings should strictly alternate
  6.  
  7. (define (append-to-car str lst)
  8.   (cond [(null? lst) (list str)]
  9.         [else (cons (string-append (car lst) "\n" str) (cdr lst))]))
  10.  
  11. (define-values (read-o read-p _)
  12.   (for/fold ([o '()] [p '()] [c "# "])
  13.     ([line (in-lines input-file)])
  14.    
  15.     (match (list (substring line 0 2) (substring line 2))
  16.       [(list "# " rest)
  17.        (cond [(equal? c "# ") (values (append-to-car rest o) p "# ")]
  18.              [else            (values (cons rest o) p "# ")])]
  19.       [(list "= " rest)
  20.        (cond [(equal? c "= ") (values o (append-to-car rest p) "= ")]
  21.              [else            (values o (cons rest p) "= ")])])))
  22.  
  23. (close-input-port input-file)
  24.  
  25. (define lines-o (list->vector (reverse read-o)))
  26. (define lines-p (list->vector (reverse read-p)))