How is Lisp's read-eval-print loop different than Python's?

后端 未结 4 2017
长情又很酷
长情又很酷 2021-01-30 15:52

I\'ve encounter a following statement by Richard Stallman:

\'When you start a Lisp system, it enters a read-eval-print loop. Most other languages have not

4条回答
  •  天命终不由人
    2021-01-30 16:21

    Stallman's point is that not implementing an explicit "reader" makes Python's REPL appear crippled compared to Lisps because it removes a crucial step from the REPL process. Reader is the component that transforms a textual input stream into the memory — think of something like an XML parser built into the language and used for both source code and for data. This is useful not only for writing macros (which would in theory be possible in Python with the ast module), but also for debugging and introspection.

    Say you're interested in how the incf special form is implemented. You can test it like this:

    [4]> (macroexpand '(incf a))
    (SETQ A (+ A 1)) ;
    

    But incf can do much more than incrementing symbol values. What exactly does it do when asked to increment a hash table entry? Let's see:

    [2]> (macroexpand '(incf (gethash htable key)))
    (LET* ((#:G3069 HTABLE) (#:G3070 KEY) (#:G3071 (+ (GETHASH #:G3069 #:G3070) 1)))
     (SYSTEM::PUTHASH #:G3069 #:G3070 #:G3071)) ;
    

    Here we learn that incf calls a system-specific puthash function, which is an implementation detail of this Common Lisp system. Note how the "printer" is making use of features known to the "reader", such as introducing anonymous symbols with the #: syntax, and referring to the same symbols within the scope of the expanded expression. Emulating this kind of inspection in Python would be much more verbose and less accessible.

    In addition to the obvious uses at the REPL, experienced Lispers use print and read in the code as a simple and readily available serialization tool, comparable to XML or json. While Python has the str function, equivalent to Lisp's print, it lacks the equivalent of read, the closest equivalent being eval. eval of course conflates two different concepts, parsing and evaluation, which leads to problems like this and solutions like this and is a recurring topic on Python forums. This would not be an issue in Lisp precisely because the reader and the evaluator are cleanly separated.

    Finally, advanced features of the reader facility enable the programmer to extend the language in ways that even macros could not otherwise provide. A perfect example of such making hard things possible is the infix package by Mark Kantrowitz, implementing a full-featured infix syntax as a reader macro.

提交回复
热议问题