Reputation: 3145
I'm writing an application (A juggling pattern animator) in PLT Scheme that accepts Scheme expressions as values for some fields. I'm attempting to write a small text editor that will let me "explode" expressions into expressions that can still be eval'd but contain the data as literals for manual tweaking.
For example,
(4hss->sexp "747")
is a function call that generates a legitimate pattern. If I eval and print that, it becomes
(((7 3) - - -) (- - (4 2) -) (- (7 2) - -) (- - - (7 1)) ((4 0) - - -) (- - (7 0) -) (- (7 2) - -) (- - - (4 3)) ((7 3) - - -) (- - (7 0) -) (- (4 1) - -) (- - - (7 1)))
which can be "read" as a string, but will not "eval" the same as the function. For this statement, of course, what I need would be as simple as
(quote (((7 3...
but other examples are non-trivial. This one, for example, contains structs which print as vectors:
pair-of-jugglers
; -->
(#(struct:hand #(struct:position -0.35 2.0 1.0) #(struct:position -0.6 2.05 1.1) 1.832595714594046) #(struct:hand #(struct:position 0.35 2.0 1.0) #(struct:position 0.6 2.0500000000000003 1.1) 1.308996938995747) #(struct:hand #(struct:position 0.35 -2.0 1.0) #(struct:position 0.6 -2.05 1.1) -1.3089969389957472) #(struct:hand #(struct:position -0.35 -2.0 1.0) #(struct:position -0.6 -2.05 1.1) -1.8325957145940461))
I've thought of at least three possible solutions, none of which I like very much.
Help me out before I start having bad recursion dreams again.
Upvotes: 2
Views: 508
Reputation: 29546
I'm not sure what you're trying to do. Specifically, trying to produce a file with eval
-able code seems like a strange choice. In any case, creating a serialization of random values is a problem when you're dealing with structs -- since there could be different structs with the same name. Some points that might be relevant for you:
There is an undocumented library scheme/fasl
that can read and write values in binary format (useful for large data).
There is also mzlib/pconvert
-- a library that DrScheme uses to print values as expressions that can be evaluated (but this won't work on all kinds of data).
If you want to use structs that are very easy to write in a readable form, then you can use "prefab" structs.
(For more details it's probably best to ask on the mailing list.)
Upvotes: 4