[Larceny-users] directory-files

William D Clinger will at ccs.neu.edu
Thu Feb 22 10:50:47 EST 2007


Sven wrote:
> So, now I am using the following definition (I hope this one will live a
> little longer):
> 
> (define call-with-input-pipe (lambda (command pred)
>   (let* ((results (process (if (string? command)
>                              command
>                              (reduce-r (lambda (arg result)
>                                          (string-append arg " " result))
>                                        command))))
>          (result (pred (car results))))
>     (close-input-port (car results))
>     (close-output-port (cadr results))
>     (unix/waitpid (caddr results))
>     result)))

For an additional increment of robustness, you might
try the following (untested) version:

(define (call-with-input-pipe command pred)
  (let ((results (process (if (string? command)
                            command
                            (reduce-r (lambda (arg result)
                                        (string-append arg " " result))
                                      command)))))
    (dynamic-wind
     (lambda () #t)
     (lambda () (pred (car results)))
     (lambda ()
       (close-input-port (car results))
       (close-output-port (cadr results))
       (unix/waitpid (caddr results))))))

The dynamic-wind will ensure that the ports get closed
and the unix/waitpid is called even if something goes
wrong during the computation performed by pred.

Will



More information about the Larceny-users mailing list