* newbie: read, load and eval
@ 2006-10-22 6:48 dale
0 siblings, 0 replies; 3+ messages in thread
From: dale @ 2006-10-22 6:48 UTC (permalink / raw)
This seems like the simplest thing which guile ought to be able to do
in a snap, but I can't for the life of me work out how to do it.
I have a file called a.data which contains some data as a scheme
expression, such as
'("This" "is" "a" "test")
and I want to use these data in a program, so I want to write a line
like
(display (load "a.data"))
but it doesn't work; nor does
(with-input-from-file "a.data" (lambda () (display (read))))
What is the way to do this?
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: newbie: read, load and eval
@ 2006-10-22 8:34 Mike Gran
2006-10-22 8:55 ` dale
0 siblings, 1 reply; 3+ messages in thread
From: Mike Gran @ 2006-10-22 8:34 UTC (permalink / raw)
Let's say you had a file "a.data" that contained
("This" "is" "a" "test")
Note that there is no initial quote.
One way would be
(display (read (open-input-file "a.data")))
but this doesn't explicity close the file "a.data" right after the call, which might be an issue if you are opening a lot of files.
One step up could be
(call-with-input-file "a.data" (lambda (p) (display (read p))))
This will explicity close the file after it is read.
Note that this specifically reads exactly one s-expression from the top of the file, as you requested. It might be simpler to have an "a.data" that contained a bunch of (define) statements, like so
(define a '("This" "is" "a" "test"))
Then you could (load "a.data") that file, and the defined variables would be available.
Hope this helps,
Mike Gran
----- Original Message ----
From: "dale@dmellor.dabsol.co.uk" <dale@dmellor.dabsol.co.uk>
To: guile-user@gnu.org
Sent: Saturday, October 21, 2006 11:48:04 PM
Subject: newbie: read, load and eval
This seems like the simplest thing which guile ought to be able to do
in a snap, but I can't for the life of me work out how to do it.
I have a file called a.data which contains some data as a scheme
expression, such as
'("This" "is" "a" "test")
and I want to use these data in a program, so I want to write a line
like
(display (load "a.data"))
but it doesn't work; nor does
(with-input-from-file "a.data" (lambda () (display (read))))
What is the way to do this?
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: newbie: read, load and eval
2006-10-22 8:34 Mike Gran
@ 2006-10-22 8:55 ` dale
0 siblings, 0 replies; 3+ messages in thread
From: dale @ 2006-10-22 8:55 UTC (permalink / raw)
Cc: guile-user
Mike Gran writes:
>>
>> Let's say you had a file "a.data" that contained
>>
>> ("This" "is" "a" "test")
>>
>> Note that there is no initial quote.
>>
>> ...
>>
>> One step up could be
>>
>> (call-with-input-file "a.data" (lambda (p) (display (read p))))
Perfect! I was basically making a mistake with the initial quote.
>> Note that this specifically reads exactly one s-expression from the
>> top of the file, as you requested. It might be simpler to have an
>> "a.data" that contained a bunch of (define) statements, like so
>>
>> (define a '("This" "is" "a" "test"))
>>
>> Then you could (load "a.data") that file, and the defined variables would be available.
Yuk! That would mean that I have two independent pieces of code
which need to agree with each other to work properly, opening the door
for bugs... it also would pollute the top-level environment. I like
your first answer!
>> Hope this helps,
Sure does! Thanks.
_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://lists.gnu.org/mailman/listinfo/guile-user
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2006-10-22 8:55 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-10-22 6:48 newbie: read, load and eval dale
-- strict thread matches above, loose matches on Subject: below --
2006-10-22 8:34 Mike Gran
2006-10-22 8:55 ` dale
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).