Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Lisp the only language with REPL?

Tags:

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. How is Lisp REPL different from non-Lisp REPL?

like image 651
Eli Schneider Avatar asked Apr 15 '11 00:04

Eli Schneider


People also ask

What languages have a REPL?

Repl.it already supports virtually every programming language you can think of, no matter whether that's JavaScript, Python, PHP or QBasic, as well as popular frameworks like Django, Ruby on Rails and Sinatra.

Is REPL a programming language?

A read–eval–print loop (REPL), also termed an interactive toplevel or language shell, is a simple interactive computer programming environment that takes single user inputs, executes them, and returns the result to the user; a program written in a REPL environment is executed piecewise.

Does 2021 still use Lisp?

Yes, it is, but you have to know where to look. People who use LISP don't tend to shout too loudly about it but there's a handful of examples of a few high-profile startups having used it to great effect over the last 20 years. It is also very popular with small companies in Europe.

Is Lisp a dead language?

LISP. One of the old languages, LISP, has lost its fame and started its journey to death. The language is being rarely used by developers these days. LISP is a language of fully parenthesised prefix notation and is the second oldest high-level programming language, developed in 1960.


2 Answers

The idea of a REPL comes from the Lisp community. There are other forms of textual interactive interfaces, for example the command line interface. Some textual interfaces also allow a subset of some kind of programming language to be executed.

REPL stands for READ EVAL PRINT LOOP: (loop (print (eval (read)))).

Each of the four above functions are primitive Lisp functions.

In Lisp the REPL is not a command line interpreter (CLI). READ does not read commands and the REPL does not execute commands. READ reads input data in s-expression format and converts it to internal data. Thus the READ function can read all kinds of s-expressions - not just Lisp code.

READ reads a s-expression. This is a data-format that also supports encoding source code. READ returns Lisp data.

EVAL takes Lisp source code in the form of Lisp data and evaluates it. Side effects can happen and EVAL returns one or more values. How EVAL is implemented, with an interpreter or a compiler, is not defined. Implementations use different strategies.

PRINT takes Lisp data and prints it to the output stream as s-expressions.

LOOP just loops around this. In real-life a REPL is more complicated and includes error handling and sub-loops, so-called break loops. In case of an error one gets just another REPL, with added debug commands, in the context of the error. The value produced in one iteration also can be reused as input for the next evaluation.

Since Lisp is both using code-as-data and functional elements, there are slight differences to other programming languages.

Languages that are similar, those will provide also similar interactive interfaces. Smalltalk for example also allows interactive execution, but it does not use a data-format for I/O like Lisp does. Same for any Ruby/Python/... interactive interface.

Question:

So how significant is the original idea of READing EXPRESSIONS, EVALuating them and PRINTing their values? Is that important in relation to what other languages do: reading text, parsing it, executing it, optionally print something and optionally printing a return value? Often the return value is not really used.

So there are two possible answers:

  1. a Lisp REPL is different to most other textual interactive interfaces, because it is based on the idea of data I/O of s-expressions and evaluating these.

  2. a REPL is a general term describing textual interactive interfaces to programming language implementations or subsets of those.

REPLs in Lisp

In real implementations Lisp REPLs have a complex implementation and provide a lot of services, up to clickable presentations (Symbolics, CLIM, SLIME) of input and output objects. Advanced REPL implementations are for example available in SLIME (a popular Emacs-based IDE for Common Lisp), McCLIM, LispWorks and Allegro CL.

Example for a Lisp REPL interaction:

a list of products and prices:

CL-USER 1 > (setf *products* '((shoe (100 euro))                                (shirt (20 euro))                                (cap (10 euro)))) ((SHOE (100 EURO)) (SHIRT (20 EURO)) (CAP (10 EURO))) 

an order, a list of product and amount:

CL-USER 2 > '((3 shoe) (4 cap)) ((3 SHOE) (4 CAP)) 

The price for the order, * is a variable containing the last REPL value. It does not contain this value as a string, but the real actual data.

CL-USER 3 > (loop for (n product) in *                   sum (* n (first (second (find product *products*                                                 :key 'first))))) 340 

But you can also compute Lisp code:

Let's take a function which adds the squares of its two args:

CL-USER 4 > '(defun foo (a b) (+ (* a a) (* b b)))  (DEFUN FOO (A B) (+ (* A A) (* B B))) 

The fourth element is just the arithmetic expression. * refers to the last value:

CL-USER 5 > (fourth *) (+ (* A A) (* B B)) 

Now we add some code around it to bind the variables a and b to some numbers. We are using the Lisp function LIST to create a new list.

CL-USER 6 > (list 'let '((a 12) (b 10)) *) (LET ((A 12) (B 10)) (+ (* A A) (* B B))) 

Then we evaluate the above expression. Again, * refers to the last value.

CL-USER 7 > (eval *) 244 

There are several variables which are updated with each REPL interaction. Examples are *, ** and *** for the previous values. There is also + for the previous input. These variables have as values not strings, but data objects. + will contain the last result of the read operation of the REPL. Example:

What is the value of the variable *print-length*?

CL-USER 8 > *print-length* NIL 

Let's see how a list gets read and printed:

CL-USER 9 > '(1 2 3 4 5) (1 2 3 4 5) 

Now let's set the above symbol *print-length* to 3. ++ refers to the second previous input read, as data. SET sets a symbols value.

CL-USER 10 > (set ++ 3) 3 

Then above list prints differently. ** refers to the second previous result - data, not text.

CL-USER 11 > ** (1 2 3 ...) 
like image 182
Rainer Joswig Avatar answered Sep 20 '22 07:09

Rainer Joswig


Seeing as the concept of a REPL is to just Read, Eval, Print & Loop it's not too suprising that there are REPLs for many languages:

C/C++

C#/LINQ

Erlang

Haskell (on windows)

Java

Javascript

Julia

Perl

Python

Ruby

Scala

Smalltalk -- I learned it on a REPL!

like image 31
Albert Perrien Avatar answered Sep 20 '22 07:09

Albert Perrien