Lisp - REPL



Introduction to REPL

REPL stands for Read Evaluate Print Loop. It is an interactive environment where we can execute lisp code line by line and check the result immediately. REPL is a powerful tool to learn Lisp, to quickly test code and debug issues.

How REPL Works?

  • Read− REPL reads an input, generally a LISP expression.

  • Evaluate− REPL evaluate the LISP expression, executes the code.

  • Print− Result is printed.

  • Loop− REPL loops back to the Read step, starts waiting for your input.

This cycle of repeated steps helps in testing LISP expressions quickly using REPL.

Popular REPL Implemenations

Following are some of the popular REPL Implementations.

  • SBCL− Steel Bank Common Lisp REPL is widely used and feature rich environment.

  • CLISP− Common Lisp REPL is one of the popular and easy to use environment.

  • EMacs Lisp− EMacs has built-in EMacs LISP REPL to interact with editor LISP environment.

We've use CLISP REPL in this chapter's examples.

Steps to Start REPL

  • Install LISP Implemenation− Install CLISP as stated in Lisp - Environment Setup chapter.

  • Open Terminal− Go to the directory where CLISP is installed.

  • Run REPL Executable− Run the clisp executable.

Running REPL

Once you open the CLISP Executable, you may see the following output.

  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]> 

Write an expression and press enter.

[1]> (+ 1 2)
3
[2]>

Print Hello World.

[2]> (format t "Hello World!")
Hello World!
NIL

Core Concepts in REPL

  • Data Types− REPL supports LISP data types like atoms, lists, symbols, numbers and strings.

  • Functions− Functions can be created and called in REPL.

  • Variables− Variable declaration is supported and their usage can be tested easily.

  • Control Flow− if, cond, loop statements can be tested easily.

  • List− Lisp manipulation, and functions like car, cdr, cons are supported.

Advance Features of REPL

  • History Support−Using history feature, already executed commands can be recalled and modified.

  • Tab completion− Tab completion helps in completing command without remembering them.

  • Documentation lookup− describe or apropos functions helps in searching documentation of the Lisp commands.

  • function.
  • Debugging− Attaching debugger with REPL can help to step through code, inspect variables, and identify errors.

  • Loading files− REPL can load lisp source file as well and we can execute them in terminal.

  • Macros− Macros can be used within REPL and we can understand how macros transform the code.

  • Packages− Lisp packages are supported to organize code and avoid naming conflicts.

Best Practices

  • Using the REPL for learning− REPL can be used to learn how to use Lisp syntax, semantics, and programming concepts effectively.

  • REPL-driven development−Test, experiment code in REPL and then development.

  • Integrating the REPL with an editor− EMacs, VI can be used to seamlessly interact with a Lisp REPL.

Advertisements