[plt-scheme] Best pattern for bringing external data into a program?

From: Dave Gurnell (d.j.gurnell at gmail.com)
Date: Wed Mar 25 15:57:50 EDT 2009

> Say that you wanted to store some program data outside of the program
> so it could be loaded later on. It is struts or lists.
>
> What is the best pattern for doing this in PLT Scheme?
>
> I have seen a few different approaches: eval, pattern matching on the
> data to bring it in, and even eval'ing in a namespace.
>
> What is the best approach?


If your structures are transparent you can just write/read them. The  
example below uses string ports but file ports are essentially the same.

I don't know whether this is the "best" approach. Is this the kind of  
thing you mean?

-- Dave

#lang scheme

(define-struct item (a b c) #:transparent)

(define original-data
   (list (make-item 1 2 3)
         (make-item 2 3 4)
         (make-item 3 4 5)))

(define data-on-disk
   (with-output-to-string
    (lambda ()
      (write original-data))))

(define loaded-data
   (read (open-input-string data-on-disk)))

original-data
data-on-disk
loaded-data


Posted on the users mailing list.