Saturday 17 November 2012

MathP is not CL Standards Compliant

Bad news! MathP, my very own mathematics notation library for Common Lisp, breaks the ANSI CL standard. The problem is that unread-char cannot be called consecutively without calling something that will read a character (like read-char). From the CL Hyperspec page for unread-char:
It is an error to invoke unread-char twice consecutively on the same stream without an intervening call to read-char (or some other input operation which implicitly reads characters) on that stream

I used it to basically peek multiple characters ahead, like peek-char, but for multiple characters. This may be why MathP doesn't work in CLISP. That allows me to have arbitrary operators. For example, I could have the following as distinct operators:
  • * for multiplication
  • ** (perhaps for exponentiation)
  • .* (perhaps for element-wise multiplication, like in MATLAB)
  • *. (dot product?)
  • *.* (perhaps for element-wise exponentiation)
  • etc.

I thought I was being clever, but such a use contravenes the standard pretty clearly. So I'm interested in alternatives. Does anyone know if it's possible to do multiple unread-chars with other stream implementations? Also, can you read lisp from them with standard reading functions?


  1. You can unread-string in this way:
    Make a concatenated stream from a string stream and *standard-input* and set the *standard-input* to it.
    Then in a pertinent moment check by CONCATENATED-STREAM-STREAMS if your string stream has been depleted and reset *standard-input* not to have a concatenated stream of concatenated stream of ... in the *standard-input* variable with time.

  2. Thanks Goheeca, I'd overlooked concatenated streams. That's a little corner of the CLHS I hadn't absorbed... It looks perfect for this type of problem.