|
From: | Jinsong Liang |
Subject: | Re: [Chicken-users] read file into a list of lists |
Date: | Tue, 12 Jul 2016 23:49:45 -0400 |
ArthurCheers,(map string->number (read-lines)))))(lambda ()(with-input-from-file filename(define (read-all-lines filename)Jinsong, the closest solution I can think of is the read-lines procedure, which returns a list of strings (each string a line read).An example would be:
http://api.call-cc.org/doc/extras/read-lines
Supposing you have a number per line, you could use string->number to get the result.On Wed, Jul 13, 2016 at 12:07 AM, Jinsong Liang <address@hidden> wrote:_______________________________________________Hi,I need to read a file (lines of numbers) into a list of lists with each line a list. I wrote the following function to do it:
(define (read-all-lines file-name)
(let ([output '()])
(let ([p (open-input-file file-name)])
(let f ([x (read-line p)])
(if (eof-object? x)
(close-input-port p)
(begin
(set! output (cons (string-split x) output))
(f (read-line p))))))
(reverse output)))I have a few questions regarding the above code:1. Is there an existing API to do the same thing?2. Using set! seems not a lispy coding style. Is it true?3. Any improvements I can make ? I bet there are tons.Thank you!Jinsong
Chicken-users mailing list
address@hidden
https://lists.nongnu.org/mailman/listinfo/chicken-users
[Prev in Thread] | Current Thread | [Next in Thread] |