Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure read-string [duplicate]

Tags:

clojure

(read-string "01") returns 1.

In fact read-string from "01" to "07" returns the correct answer. But while we do (read-string "08") it throws an error:

NumberFormatException Invalid number: 08 clojure.lang.LispReader.readNumber (LispReader.java:330).

Can anyone plese help me to figure out why?"

like image 969
Aman shah Avatar asked Dec 21 '25 17:12

Aman shah


2 Answers

If the string starts with a 0, then the number is read as an octal number: https://www.ascii.cl/conversion.htm

Thus, the table below, with some sample conversions:

  • 01 -> 1
  • 02 -> 2
  • 0117 -> 79
  • 0200 -> 128
  • 08 -> invalid
like image 73
Nicolas Modrzyk Avatar answered Dec 24 '25 12:12

Nicolas Modrzyk


If you had kept going you may have seen:

> (read-string "010")
8

Literal numbers beginning with 0 are taken to be octal (base 8). Hence the digit 8 is not valid.

like image 20
jas Avatar answered Dec 24 '25 11:12

jas



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!