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

后端 未结 4 1953
长情又很酷
长情又很酷 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:35

    In a Lisp-based system one typically develops the program while it is running from the REPL (read eval print loop). So it integrates a bunch of tools: completion, editor, command-line-interpreter, debugger, ... The default is to have that. Type an expression with an error - you are in another REPL level with some debugging commands enabled. You actually have to do something to get rid of this behavior.

    You can have two different meanings of the REPL concept:

    • the Read Eval Print Loop like in Lisp (or a few other similar languages). It reads programs and data, it evaluates and prints the result data. Python does not work this way. Lisp's REPL allows you to work directly in a meta-programming way, writing code which generates (code), check the expansions, transform actual code, etc.. Lisp has read/eval/print as the top loop. Python has something like readstring/evaluate/printstring as the top-loop.

    • the Command Line Interface. An interactive shell. See for example for IPython. Compare that to Common Lisp's SLIME.

    The default shell of Python in default mode is not really that powerful for interactive use:

    Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
    [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
    Type "help", "copyright", "credits" or "license" for more information.
    >>> a+2
    Traceback (most recent call last):
    File "", line 1, in 
    NameError: name 'a' is not defined
    >>> 
    

    You get an error message and that's it.

    Compare that to the CLISP REPL:

    rjmba:~ joswig$ clisp
      i i i i i i i       ooooo    o        ooooooo   ooooo   ooooo
      I I I I I I I      8     8   8           8     8     o  8    8
      I  \ `+' /  I      8         8           8     8        8    8
       \  `-+-'  /       8         8           8      ooooo   8oooo
        `-__|__-'        8         8           8           8  8
            |            8     o   8           8     o     8  8
      ------+------       ooooo    8oooooo  ooo8ooo   ooooo   8
    
    Welcome to GNU CLISP 2.49 (2010-07-07) 
    
    Copyright (c) Bruno Haible, Michael Stoll 1992, 1993
    Copyright (c) Bruno Haible, Marcus Daniels 1994-1997
    Copyright (c) Bruno Haible, Pierpaolo Bernardi, Sam Steingold 1998
    Copyright (c) Bruno Haible, Sam Steingold 1999-2000
    Copyright (c) Sam Steingold, Bruno Haible 2001-2010
    
    Type :h and hit Enter for context help.
    
    [1]> (+ a 2)
    
    *** - SYSTEM::READ-EVAL-PRINT: variable A has no value
    The following restarts are available:
    USE-VALUE      :R1      Input a value to be used instead of A.
    STORE-VALUE    :R2      Input a new value for A.
    ABORT          :R3      Abort main loop
    Break 1 [2]> 
    

    CLISP uses Lisp's condition system to break into a debugger REPL. It presents some restarts. Within the error context, the new REPL provides extended commands.

    Let's use the :R1 restart:

    Break 1 [2]> :r1
    Use instead of A> 2
    4
    [3]> 
    

    Thus you get interactive repair of programs and execution runs...

提交回复
热议问题