开发者

Print lisp function readably

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 *print-readably* is set to 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.


Functions are compiled, and their source code is never stored. I'd recommend using Emacs + Slime, it is easy to browse your history there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜