ArticleslgStudy

computer science

Read–eval–print loop

Read–eval–print loop is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Read–eval–print loop rather than just read about it. In short: 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. The term usually refers to programming interfaces similar to the classic Lisp machine interactive environment or to Common Lisp wit…

Key takeaways

  • Read–eval–print loop belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Read–eval–print loop to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Read–eval–print loop from memory before moving on to harder problems.

Reference excerpt

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. The term usually refers to programming interfaces similar to the classic Lisp machine interactive environment or to Common Lisp with the SLIME development environment. Common examples include command-line shells and similar environments for programming languages, and the technique is very characteristic of scripting languages, even though their characteristics can vary greatly.

History The expression READ-EVAL-PRINT cycle is used by L. Peter Deutsch and Edmund Berkeley for a 1964 implementation of Lisp on the PDP-1. Just one month later, Project Mac published a report by Joseph Weizenbaum (the creator of ELIZA, the world's first chatbot) describing a REPL-based language, called OPL-1, implemented in his Fortran-SLIP language on the Compatible Time Sharing System (CTSS). The 1974 Maclisp reference manual by David A. Moon attests "Read-eval-print loop" on page 89, but does not use the acronym REPL. Since at least the 1980s, the abbreviations REP Loop and REPL are attested in the context of Scheme.

Overview In a REPL, the user enters one or more expressions (rather than an entire compilation unit) and the REPL evaluates them and displays the results. The name read–eval–print loop comes from the names of the Lisp primitive functions which implement this functionality:

The read function accepts an expression from the user, and parses it into a data structure in memory. For instance, the user may enter the s-expression (+ 1 2 3), which is parsed into a linked list containing four data elements. The eval function takes this internal data structure and evaluates it. In Lisp, evaluating an s-expression beginning with the name of a function means calling that function on the arguments that make up the rest of the expression. So the function + is called on the arguments 1 2 3, yielding the result 6. The print function takes the result yielded by eval, and prints it out to the user. If it is a complex expression, it may be pretty-printed to make it easier to understand. The development environment then returns to the read state, creating a loop, which terminates when the program is closed. REPLs facilitate exploratory programming and debugging because the programmer can inspect the printed result before deciding what expression to provide for the next read. The read–eval–print loop involves the programmer more frequently than the classic edit–compile–run–debug cycle. Because the print function outputs in the same textual format that the read function uses for input, most results are printed in a form that could be copied and pasted back into the REPL. However, it is sometimes necessary to print representations of elements that cannot sensibly be read back in, such as a socket handle or a complex class instance. In these cases, there must exist a syntax for unreadable objects. In Python, it is the <__module__.class instance> notation, and in Common Lisp, the #<whatever> form. The REPL of CLIM, SLIME, and the Symbolics Lisp Machine can also read back unreadable objects. They record for each output which object was printed. Later when the code is read back, the object will be retrieved from the printed output. REPLs can be created to support any text-based language. REPL support for compiled languages is usually achieved by implementing an interpreter on top of a virtual machine which provides an interface to the compiler. For example, starting with JDK 9, Java included JShell as a command-line interface to the language. Various other languages have third-party tools available for download that provide similar shell interaction with the language, although the features can vary greatly.

Uses As a shell, a REPL environment allows users to access relevant features of an operating system in addition to providing access to programming capabilities. The most common use for REPLs outside of operating system shells is for interactive prototyping. Other uses include mathematical calculation, creating documents that integrate scientific analysis (e.g. IPython), interactive software maintenance, benchmarking, and algorithm exploration.

Lisp specifics

Implementation A minimal definition in Common Lisp is: where read waits for user input and eval evaluates it. print prints the result, and loop loops indefinitely. You can enter (+ 1 1) and stop the loop with C-c.

Functionality Typical functionality provided by a Common Lisp REPL includes:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Read–eval–print loop

Start with the simplest possible case. Write down what Read–eval–print loop claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Read–eval–print loop before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Read–eval–print loop ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Read–eval–print loop

In research
Read–eval–print loop appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Read–eval–print loop in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Read–eval–print loop is common in secondary-school and first-year university syllabi. It links to neighbouring topics Command shells, Interpreters (computing), Lisp (programming language), so understanding it makes those chapters shorter.
In everyday life
Look for Read–eval–print loop outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Read–eval–print loop” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Read–eval–print loop in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Read–eval–print loop means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Read–eval–print loop out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Read–eval–print loop in simple terms?

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. The term us…

Why does Read–eval–print loop matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Read–eval–print loop?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Read–eval–print loop.

Tags

  • Command shells
  • Interpreters (computing)
  • Lisp (programming language)
  • User interface techniques

Keep exploring