2 ms·
Ask HN: Why is #e#x+e#s+e-e#l-e a valid number in Racket?
Eli Barzley pointed out that Racket reads #e#x+e#s+e-e#l-e as a number.[1]
$ racket
Welcome to Racket v6.10.
> #e#x+e#s+e@-e#l-e
16140901064495857664-50176i
Why is it valid? According to https://docs.racket-lang.org/reference/reader.html https://docs.racket-lang.org/reference/reader.html, #s should be a structure literal.
I suppose this would be better as a StackOverflow question, but it's an interesting puzzle.
[1]
https://stackoverflow.com/questions/3146771/building-lisp-scheme-like-parse-tree-with-flex-bison#comment3234337_3146771 https://stackoverflow.com/questions/3146771/building-lisp-sc...
- gus_massa 9y agoIt's usually faster to submit it to the mailing list. [spoiler alert] The #s is brilliantly misleading. A shorter version is #x0#s0 ;==> 0.0f0 (almost like 0.0) In this case, the second # means an unspecified digit in an inexact number. For example 4## ;==> 400.0 (other scheme implementation may give a different result near 400, like 450.0) The s is the exponent mark in a single precision number 2s4 ;==> 20000.0f0
- soegaard 9y agoFirst of all the `@` means we are dealing with a an inexact ‹inexact-complexn› ::= ‹inexact-realn› @ ‹inexact-realn› Here is a short guide: #e means exact #x means hexadecimal So `#e#x+e` means exact 14. The last `#` in means an unknown number - Racket has chosen to use 0. Therefore `#e#x+e#` means 1416 = 224. The `s0` is an exponent mark, which marks the precision of an inexact number. The `s0` means single-precision (in Scheme s means short - in many implementations short and single both use single precision). Thus `#x+e#s0` means `224.0f` and `#e#x+e#s0` means 224. Now since it is a hexadecimal number the following `+e` means 14. Therefore `#e#x+e#s0+e` means `22416^14` which is 16140901064495857664. Now we come the `@`. This means the first part was the real part of a complex number and what follows is the imaginary part.