I'm trying out lisp and working through the book Practical Common Lisp at http://www.gigamonkeys.com/book/.
Is there a command which can print a function which was previously defined at the REPL in a way that can later be read? I've tried
(print #'function-name)
but this results in output enclosed in #<>
which can't be read back in (and if
is set to *print-readably
*T
I just get an error). I get the same results using princ
or prin1
instead of print
.
I'm a bit surprised this isn't easy to find. Several of the things I've read on lisp encourage experimentation at the REPL, but if you can't save your functions after you've entered them, you're forced to write them in a separate file before entering them, which partly defeats the point.
Function objects can't be read back.
1) You can use the function DRIBBLE, which causes the REPL input to be saved into a file.
2) Evaluating from an editor into the REPL is common. You don't need to type to the REPL, you can type into a editor text buffer and send the expressions from there to the REPL.
3) Common Lisp has the FUNCTION-LAMBDA-EXPRESSION, which under some circumstance might be able to recover the source code (or what the Lisp system has recorded as source):
CL-USER 200 > (defun foo (a) (+ a b))
FOO
CL-USER 201 > (function-lambda-expression #'foo)
(LAMBDA (A) (DECLARE (SYSTEM::SOURCE-LEVEL #<EQ Hash Table{0} 41C00751D3>))
(DECLARE (LAMBDA-NAME FOO))
(+ A B))
NIL
FOO
In short, no there isn't, however, the opposite is easy - it is very easy to write them in a file and send them to a repl using emacs & slime.
By default iirc C-x e
evaluates the form immediately before point (the emacs cursor) in the repl.
This gives you the best of both worlds - consistency of files and interactivity of the repl.
Good luck with your lisp journey!
I would recommend you use emacs. Load the pretty printer function. The package is (quote pp). Just apropos pretty printer. Slime also does prettty printing of functions (if I remeber correctly) but the package with emacs is nice.
(require 'pp)
I've written a format statement that I used to print anonymous (or named) functions, but the code is on the other machine. If you're interested let me know and I'll try and find it.
[...]
yes you can find and print functions typed at the REPL at a later time, just check the namespace for the function name. Think of the name space as a hash table. If the function has been interned then it should show up in the namespace. I think there is a way to say, maphash the namespace...
Some implementations of lisp require you to set a variable to keep the function code attached to the function symbol; this is like debugging symbols in c. It takes up space but debugging auto-generated functions is a bitch with tossing in some macro-1 expands...
Try using the commands , , ... and I'm drawing a blank. It's been a while. Usually the biggest problem is fixing the un-named functions (a lambda expression). Some implementations also implement the "named anonymous function" or something like that.
< Is there a command which can print a function which was previously defined at the REPL in < a way that can later be read?
A really nice question. Yes you can. It maybe as simple as write-fasl-stream or something a bit more involved.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With