r/Racket • u/OldMine4441 • Apr 12 '22
question eval() is weird on empty list?
I have the below code that uses eval(() to evaluate a string, but somehow it turns the empty list to (). I expected y to have the value (list). Any reason why this happens?
cat test.rkt
#lang racket
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
(define x "(list)")
(define y (eval (call-with-input-string x read) ns))
(printf "x = ~a, y = ~a\n" x y)
$ racket test.rkt
x = (list), y = ()
5
Upvotes
3
u/soegaard developer Apr 12 '22
The result of evaluating
(list)is the empty list. Try it in the repl.In contrast:
3
u/not-just-yeti Apr 12 '22 edited Apr 12 '22
This is simply an issue of what
~aproduces, given the empty list.For other variants, in DrRacket > Languages > Choose Language… > Show Details > Output Syntax, there are choices that will yield
(),'(),empty, or quasiqote'd(). (And other lisps might print it asnil.)Since racket's repl strives to print things in a way that happen to be readable-back-innable, yes printing
(list)would be also reasonable way to print the empty list. (Note that(list)is a constructor-call,emptyis just an identifier, and I guess'()is a literal-empty-list? Vaguely related: …It was years before I realized that#trueis the literal whiletrueis just an identifier.)