Suppose I have a collection of external data structures in a txt file. Whenever I need the data for computation I have to parse the file and then use it's values. How to make it more convenient?
- Keep the once parsed data as a complex list structure and write it in a SCM file with
write. Then the full list can be simply read withread. Sure, but... (everytime the complex list representation of the data would have to be read and all the records would have to be formed afterwards - such could be avoided if the data had already been compiled) - Can't I somehow keep the parsed result in a guile module? That is a compiled stuff once for all? I cannot find the right way to do it, though.
A simplified example:
Let the complex data be just a single number in an external file DATA.txt:
10
Let's create a module data.scm that reads in the external data, stores it and exports it:
(define-module (data))
(export DATA)
(define DATA (with-input-from-file "DATA.txt" read))
Let's test this approach with test.scm:
(add-to-load-path (dirname (current-filename)))
(use-modules (data))
(format #t "DATA: ~a\n" DATA)
THIS SEEMS TO WORK (auto-compilation is on or even guild compile data.scm is used):
$ guile test.scm
DATA: 10
However, when I delete the DATA.txt file, an error is raised since the (compiled) data module is missing the DATA.txt file for reading!
So the problem prevails: How to store the external data in the module at compile-time, actually?
WHAT WORKS - BUT IS UGLY!
To generate the module file literally, like:
(define DATA (with-input-from-file "DATA.txt" read))
(with-output-to-file "data.scm"
(lambda ()
(format #t
"(define-module (data))
(export DATA)
(define DATA ~a)\n"
DATA)))
This approach is like using the C macros and not taking advantage of the scheme's macros (i.e. syntax). Is there really no elegant scheme way?
guild compile. So I'd look into your second option (by putting your data into its own module and importing it where needed)