Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Read idiom

44 views
Skip to first unread message

WJ

unread,
Nov 7, 2012, 5:52:39 PM11/7/12
to
Marco Antoniotti wrote:

> (defun read-some-data-from-file (filename)
> (with-open-file (file filename)
> (let ((n-items (read file)))
> (loop for i from 0 below n-items
> collect (float (read file))))))
>
> So, here we are: making similar assumptions to those evident in the
> tllotbafir (nee` Python) program, you have a CL program that does the
> same stuff plus ensuring that the file gets closed in case of error.
> Moreover the CL program works for a file such as
>
> data1.dat
> line 0: 10 1 2 3 4 5 6 7 8 10 9
>
> or as a file like
>
> data2.dat
> line 0: 10
> line 1:
> line 2:
> .
> .
> .
> line N: 1 2 3 4 5 6 7 8 10 9
>
> Just to clarify...
>
> * (read-some-data-from-file "data1.dat")
> (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 10.0 9.0)
> * (read-some-data-from-file "data2.dat")
> (1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 10.0 9.0)
> *
>
> So, in the simple case, CL is as concise (the CL program is shorter)
> and elegant as you can get

No, it isn't.

Racket:

(define (read-some-data-from-file filename)
(with-input-from-file filename (thunk
(define n-items (read))
(for/list ([i n-items]) (real->double-flonum (read))))))

Marco Antoniotti

unread,
Nov 8, 2012, 1:10:34 PM11/8/12
to
What is so different from

(defun read-some-data-from-file (filename)
(with-open-file (f filename :direction :input)
(let ((n (read f)))
(for/list ((i n)) (float (read f))))))

?

Oh. Yes. I forgot..... not to answer. :(

MA

WJ

unread,
Jan 3, 2015, 4:18:36 AM1/3/15
to
Gauche Scheme:

(use srfi-42) ;; Eager comprehensions.
(with-input-from-file "data.txt" (lambda()
(list-ec (: i (read)) (inexact (read)))))

WJ

unread,
Dec 12, 2015, 2:07:46 AM12/12/15
to
Why this is marked as abuse? It has been marked as abuse.
Report not abuse
Why does the file have to contain a count of the numbers
in the file? Why not just read the whole file?

MatzLisp (Ruby):

If the file isn't too large:

IO.read('Sherlock-data-1.txt').split.map(&:to_f)
==>[4.0, 3.0, 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 13.0, 29.0, 71.0]

--
You have politicians saying that ... as many Africans as want to come into
Sweden should be able to come.... They've already said that everybody from
Syria can come to Sweden.... [T]hey are actually thinking of commandeering
people's vacation homes because they need more housing for immigrants.
--- Dr. Kevin MacDonald (http://lnrlive.com/tpc/tpc20150509c.mp3)
0 new messages