Reputation: 33
In short, what's the difference?
(setq var1 `(,(get-internal-real-time)))
var1
-->(1358995178904535)
var1
-->(1358995178904535)
(setq var2 '(#.(get-internal-real-time)))
var2
-->(1358995195568422)
var2
-->(1358995195568422)
I thought that perhaps by "read-time eval" it meant that it would evaluate each time I read the variable, but I guess I was wrong, and quasiquote eval doesn't do that either.
Upvotes: 3
Views: 602
Reputation: 139241
If you want to see read-time effects in isolation, using the REPL evaluating forms is not a good idea. REPL means READ EVAL PRINT LOOP. Every piece of code will be read, evaluated and print. Not just read.
Instead see this:
CL-USER > (read-from-string "`(,(get-internal-real-time))")
(LIST (GET-INTERNAL-REAL-TIME))
Above result depends a bit on the implementation, because the read version of a backquote list is not defined. But the effect is similar: the resulting form returned from the Lisp reader, is a call to LIST
(or an equivalent) with the sub-form as an argument.
CL-USER > (read-from-string "'(#.(get-internal-real-time))")
(QUOTE (465370171))
Above executes the form at read-time and includes the value into the expression, which is the result of the read operation.
Upvotes: 4
Reputation: 21238
An example where the difference matters:
* (defun foo () `(,(get-internal-real-time)))
FOO
* (defun bar () '(#.(get-internal-real-time)))
BAR
* (foo)
(44577)
* (foo)
(47651)
* (bar)
(41929)
* (bar)
(41929)
As you can see, when you're not using the value directly (as in the (setq var1 ...)
case), the quasi-quote is expanded each time, returning different values. However, with the read-time eval, it's only called once, returning the same value again and again.
Upvotes: 4
Reputation: 9451
"read-time eval" means, that it would be evaluated each time the code itself is read. When you enter var2
in the REPL you don't read a variable, you access its value. So, in your case both forms produce the same result.
Upvotes: 3