More on Another Encoding of S-expressions in Symbols
In previous post, I have shown another interesting method for encoding of S-expressions into symbols. In this post, I'll show how it can be implemented in Lisp dialects that print symbols on different way from S-expressions, i.e. encapsulated in vertical bars or other symbols (Common Lisp, ISLisp, Picolisp, some implementations of Scheme). This is, normally, good feature. However, repeated encoding of the symbols results in exponential growth. So, we must get rid of those vertical bars inside our symbols.The code, here in Common Lisp, is sort of trickier than it is in Newlisp or it would be in Clojure. (defun decode-deep (r) (cond ((symbolp r) (let((result (read-from-string (string r)))) (if (symbolp result) result (decode-deep result)))) ((listp r) (mapcar (quote decode-deep) r)))) (defun sexpr->symbol (r) (make-symbol (write-to-string (list 'q (decode-deep r))))) (defun encode-deep (r) (cond ((symbolp r) r) ((listp r)(if (eq (first r) (quote q)) (make-symbol (write-to-string r)) (mapcar (quote encode-deep) r))))) (defun strip-q (r) (first (rest r))) (defun symbol->sexpr (r) (encode-deep (strip-q (read-from-string (string r))))) ------------- [43]> (setq q0 (sexpr->symbol (list (quote b) (quote c)))) #:|(Q (B C))| [44]> (setq s (sexpr->symbol (list (quote a) q0))) #:|(Q (A (Q (B C))))| [45]> (setq s0 (symbol->sexpr s)) (A #:|(Q (B C))|) |
More on Another Encoding of S-expressions in Symbols.
.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment