[plt-scheme] help writing idiomatic plt scheme

From: Veer (diggerrrrr at gmail.com)
Date: Sun Jun 15 15:14:02 EDT 2008

I might as well try :

;;multi-filter : (string->boolean) port -> (list (listof string)
(listof string) string)
;;to produce a list such that it simulates (list (filter f lst)
(filter neg-f lst) extra)
(define (multi-filter f in)
  (local ((define (multi-filter-accum in L R S)
            (local ((define a-line (read-line in)))
                [(eof-object? a-line) (list L R S)]
                [(f a-line) (multi-filter-accum in (cons  a-line L) R
                                                (string-append S
(substring a-line 0 1)))]
                [else (multi-filter-accum in L (cons a-line R)
                                          (string-append S (substring
a-line 0 1)))]))))
    (multi-filter-accum in empty empty "")))

(local ((define ALL (multi-filter (lambda (s) (char=? (string-ref s 0) #\#))
                                        (open-input-file "line-ex")))
        (define line-# (map (lambda (s) (substring s 1)) (first ALL)))
        (define line-= (map (lambda (s) (substring s 1)) (second ALL)))
        (define str-acc (third ALL)))
  (list line-# line-= str-acc))

If only  S could be somehow made optional or not reqd then
multi-filter can be useful in other cases i guess.


Posted on the users mailing list.