Common Lisp, 58 characters
#1=(let((*print-circle* t))(print'(write '#1# :circle t)))
... or 24 characters if you don't mind assuming *print-circle*
is globally set to T
:
#1=(print '(write '#1#))
The printed representation of the code is read as a cyclic structure, where #1#
points back to the cons cell following #1=
. We quote programs so that they are not executed. Since *print-circle*
is T, the REPL takes care to emit such reader variables during printing; this is what the above code prints, and returns:
#1=(write '(print '#1#))
When we evaluate the above code, it prints:
#1=(print '(write '#1#))
If you want to stick with the default value for *print-circle*
, which is NIL in a conforming implementation, then you'll have to rebind the variable temporarily:
#1=(let((*print-circle* t))(print'(write '#1# :circle t)))
Inside the body of the LET, we print things with *print-circle*
being T. So we obtain:
#1=(write
'(let ((*print-circle* t))
(print '#1#))
:circle t)
As you can see, the new program doesn't rebind *print-circle*
, but since we are using write
, which is the low-level function called by print
, we can pass additional arguments such as :circle
. The code then works as expected:
#1=(let ((*print-circle* t))
(print '(write '#1# :circle t)))
However, you need to execute the above programs as a script, not inside a REPL, because even though you print things while taking care of circular structures, both write
and print
also returns the value being printed; and in a default REPL, the value is also being printed, but outside of the dynamic context where *print-circle*
is T.
8Related. – Martin Ender – 2017-07-04T11:42:26.663
5"Do not use random functions."? What do you mean? Functions that output a random number? – Mr. Xcoder – 2017-07-04T13:00:33.517
2Related Folklore – Ray – 2017-07-04T22:53:38.360
I'm pretty sure you don't really mean stdin is closed. This blows up some environments as stdin becomes a duplicate of the first opened file. Anyway, if you don't fix it I will abuse it. – Joshua – 2017-07-05T21:04:55.677