Is Lisp the only language with REPL?

前端 未结 7 1885
失恋的感觉
失恋的感觉 2020-12-13 06:59

There are languages other than Lisp (ruby, scala) that say they use REPL (Read, Eval, Print, Loop), but it is unclear whether what is meant by REPL is the same as in Lisp. H

7条回答
  •  感动是毒
    2020-12-13 07:36

    I think it is interesting to compare two approaches. A bare bones REPL loop in a Lisp system would look like this:

    (loop (print (eval (read))))
    

    Here are two actual Forth implementations of a REPL loop. I'm leaving nothing out here -- this is the full code to these loops.

    : DO-QUIT   ( -- )  ( R:  i*x -- )
        EMPTYR
        0 >IN CELL+ !   \ set SOURCE-ID to 0
        POSTPONE [
        BEGIN           \ The loop starts here
            REFILL      \ READ from standard input
        WHILE
            INTERPRET   \ EVALUATE  what was read
            STATE @ 0= IF ."  OK" THEN  \ PRINT
            CR
        REPEAT
    ;
    
    : quit
      sp0 @ 'tib !
      blk off
      [compile] [
      begin
        rp0 @ rp!
        status
        query           \ READ
        run             \ EVALUATE
        state @ not
        if ." ok" then  \ PRINT
      again             \ LOOP
    ;
    

    Lisp and Forth do completely different things, particularly in the EVAL part, but also in the PRINT part. Yet, they share the fact that a program in both languages is run by feeding its source code to their respective loops, and in both cases code is just data (though in Forth case it is more like data is also code).

    I suspect what anyone saying only LISP has a REPL is that the READ loop reads DATA, which is parsed by EVAL, and a program is created because CODE is also DATA. This distinction is interesting in many respects about the difference between Lisp and other languages, but as far as REPL goes, it doesn't matter at all.

    Let's consider this from the outside:

    1. READ -- returns input from stdin
    2. EVAL -- process said input as an expression in the language
    3. PRINT -- print EVAL's result
    4. LOOP -- go back to READ

    Without going into implementation details, one can't distinguish a Lisp REPL from, for example, a Ruby REPL. As functions, they are the same.

提交回复
热议问题