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

From: Ryan Culpepper (ryanc at ccs.neu.edu)
Date: Wed Mar 25 18:29:56 EDT 2009

Dave Gurnell wrote:
>> 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.

Not true---see below. Transparency isn't enough. Perhaps you were 
thinking of "prefab structs"? They can be written and read back again.

> 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

'original-data' and 'loaded-data' print the same, but they aren't the 
same kind of data. The first is a list of item structs, but the second 
is a list of vectors.

(andmap item? original-data) => #t
(andmap item? loaded-data) => #f

(andmap vector? original-data) => #f
(andmap vector? loaded-data) => #t

Change the '#:transparent' to '#:prefab', though, and it works.

Ryan


> _________________________________________________
>  For list-related administrative tasks:
>  http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.