[plt-scheme] reading a whole file

From: Jens Axel Soegaard (jensaxel at soegaard.net)
Date: Tue Nov 4 14:29:12 EST 2008

Eli Barzilay wrote:
> On Nov  4, Jens Axel Soegaard wrote:
>   
>> But what would you use instead of an accumulator?
>>     
>
> How about this:
>
>   (define (readlines filename)
>     (call-with-input-file filename
>       (lambda (p) (for/list ([x (in-lines p)]) x))))
>   
> Which conveniently throws the accumulator to a different place...
>
>   
Short and to the point. (Note that the original is from 2004, and
thus for/list wasn't available then).

For the functionally inclined, here is srfi-1 solution:

(define (readlines filename)
(with-input-from-file filename
(λ () (unfold eof-object? values (λ (s) (read-line)) (read-line)))))

Which also hides the accumulator...


-- 
Jens Axel Søgaard,
watching the election coverage



Posted on the users mailing list.