Monday, September 10, 2012

Reversible Lisp reader

The REPL loop that defines traditional list development is described by intertwining an eval operation between read and print operations, where read and print are inverses of one another. Unfortunately, most Lisp implentations do not take into account the reversibility of read and print, because their read implementations lose the whitespace around terms whenever they are called. We can rectify this by storing whitespace as representation metadata:
(with-meta '(1 2 3) {:ws ["  " "," "," "  "]})
Using this method we can convert from that sequence data structure to and from its corresponding string value:
"(  1,2,3  )"
This could be used to improve structured programming, for example you could reverse the list in place without losing the corresponding whitespace:
"(  3,2,1  )"
The whitespace was left unchanged by the rearrangement of elements. Mrging these two collections will just involve concatenating the strings in between the parenthesis of each of these structures:
"(  1,2,3    3,2,1  )"
This could also be used to switch between a visual interface to Lisp code and a textual interface, which hopefully will be an important part of Lisp development at some point in the future.

3 comments:

  1. What is the right way to preserve whitespace when you are doing things like sorting and mergeing lists?

    ReplyDelete
    Replies
    1. Leave the whitespace unchanged under the reordering of elements and merge the whitespace under the merging of elements. I added an example.

      Delete
    2. so if we split the merged list into two unequal parts we would get this?
      "( 1,2,)'
      "(3 3,2,1 )"

      

It seems consistant and simple, even though it irritates my sense of propriety to have asysmetrical whitespace.

      Delete