Hi Arthur,

I need a list of lists because each line has more than one number. You
first solution works for me. All I need to do is to add an extra
string-split call, which returns a list.

Thanks a lot!

Jinsong

On Tue, Jul 12, 2016 at 11:52 PM, Arthur Maciel <arthurmac...@gmail.com>
wrote:

> Although I can't imagine why you'd need it, but in order to get list of
> lists you could do:
>
> (define (read-all-lines filename)
>    (with-input-from-file filename
>       (lambda ()
>          (map (lambda (x)
>                        (list (string->number x)))
>                     (read-lines)))))
>
> I'm not sure that's what you want.
>
> Cheers,
> Arthur
>
>
> On Wed, Jul 13, 2016 at 12:49 AM, Jinsong Liang <jinsongli...@gmail.com>
> wrote:
>
>> Hi Arthur,
>>
>> This simplifies my code a lot! I will give it a try.
>>
>> Thank you!
>>
>> Jinsong
>>
>> On Tue, Jul 12, 2016 at 11:45 PM, Arthur Maciel <arthurmac...@gmail.com>
>> wrote:
>>
>>> Jinsong, the closest solution I can think of is the read-lines
>>> procedure, which returns a list of strings (each string a line read).
>>>
>>> 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.
>>>
>>> An example would be:
>>>
>>> (define (read-all-lines filename)
>>>    (with-input-from-file filename
>>>       (lambda ()
>>>          (map string->number (read-lines)))))
>>>
>>> Cheers,
>>> Arthur
>>>
>>>
>>> On Wed, Jul 13, 2016 at 12:07 AM, Jinsong Liang <jinsongli...@gmail.com>
>>> 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
>>>> Chicken-users@nongnu.org
>>>> https://lists.nongnu.org/mailman/listinfo/chicken-users
>>>>
>>>>
>>>
>>
>
_______________________________________________
Chicken-users mailing list
Chicken-users@nongnu.org
https://lists.nongnu.org/mailman/listinfo/chicken-users

Reply via email to