[racket] reading and writing sets

From: Tim Brown (tim.brown at cityc.co.uk)
Date: Thu May 1 05:30:33 EDT 2014

I wish to persist "set"s to a file, for reading at a later date. However,
read and write don't seem to serialise sets (I think it's more read doesn't
deserialise them) correctly:

-| test-read-write.rkt |----------
(define (write->string->read writer reader v)
   (with-handlers ((exn? displayln))
   (define s (with-output-to-string (λ () (writer v))))
   (printf "string representation through ~s: ~s~%" writer s)
   (with-input-from-string s reader)))

(write->string->read write read (set 1 2 3))
(set? (write->string->read write read (set 1 2 3)))
(write->string->read print read (set 1 2 3))
(set? (write->string->read print read (set 1 2 3)))
(write->string->read display read (set 1 2 3))
(set? (write->string->read display read (set 1 2 3)))

-| out |--------------------------
string representation through #<procedure:write>: "#<set: 1 2 3>"
#(struct:exn:fail:read read: bad syntax `#<' #<continuation-mark-set> 
(#(struct:srcloc #f #f #f 1 2)))
string representation through #<procedure:write>: "#<set: 1 2 3>"
#(struct:exn:fail:read read: bad syntax `#<' #<continuation-mark-set> 
(#(struct:srcloc #f #f #f 1 2)))
#f
string representation through #<procedure:print>: "(set 1 2 3)"
'(set 1 2 3)
string representation through #<procedure:print>: "(set 1 2 3)"
#f
string representation through #<procedure:display>: "#<set: 1 2 3>"
#(struct:exn:fail:read read: bad syntax `#<' #<continuation-mark-set> 
(#(struct:srcloc #f #f #f 1 2)))
string representation through #<procedure:display>: "#<set: 1 2 3>"
#(struct:exn:fail:read read: bad syntax `#<' #<continuation-mark-set> 
(#(struct:srcloc #f #f #f 1 2)))
#f
-| fin |--------------------------

Can the reader read sets, or can one of the writers write them readably?

Since these sets are buried within a compound structure, blanket-
conversion to/from lists (or some other serialisable type) is, at the very
least, inconvenient.

Tim

-- 
Tim Brown <tim.brown at cityc.co.uk>  | City Computing Limited            |
T: +44 20 8770 2110                | City House, Sutton Park Road      |
F: +44 20 8770 2130                | Sutton, Surrey, SM1 2AE, GB       |
-----------------------------------------------------------------------|
BEAUTY:  What's in your eye when you have a bee in your hand           |
-----------------------------------------------------------------------'
City Computing Limited registered in London No. 1767817.
Registered Office: City House, Sutton Park Road, Sutton, Surrey, SM1 2AE
VAT number 372 8290 34.



Posted on the users mailing list.