Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert superscript 2 (²) symbol to string

I am trying to get a string of code from some lisp files, to create unix filenames and display it on web pages:

(let ((code "(defun ² (x) (* x x))"))
 (second (read-from-string code)))

Which evaluates to |2|. In fact, just typing evaluates to |2|, instead of |²|.

The code string is read from a file. I'm using SBCL 2.2.10.

With this scheme, the string "x²" is parsed the same as the string "x2" :

(read-from-string "x²")
(read-from-string "x2")

How can I make read-from-string correct, without modification on the code in the string ?

like image 286
Fnifni Avatar asked Oct 27 '25 02:10

Fnifni


1 Answers

See sect. 7.1.2 of SBCL manual. By default, SBCL extends the reader to normalize all symbols. You can prevent this for current readtable by

(setf (sb-ext:readtable-normalization *readtable*) nil)

like image 88
Tomas Zellerin Avatar answered Oct 31 '25 09:10

Tomas Zellerin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!