How to use the command 'sbcl' (with examples)
SBCL is a high-performance Common Lisp compiler that can be used to start a REPL (Read-Eval-Print Loop) interactive shell or to execute Lisp scripts. This article will provide examples of using the sbcl
command for both use cases.
Use case 1: Start a REPL (interactive shell)
Code:
sbcl
Motivation: Starting a REPL allows you to interactively evaluate Lisp expressions and test code snippets in real-time. This is useful for prototyping, debugging, and exploring the functionality of a program.
Explanation:
When simply typing sbcl
in the terminal, the command is executed without any arguments. This launches the SBCL compiler and starts the REPL, where you can enter and evaluate Lisp expressions.
Example output:
This is SBCL 2.2.11, an implementation of ANSI Common Lisp.
More information about SBCL is available at <http://www.sbcl.org/>.
* (+ 2 3)
5
* (defun square (x) (* x x))
SQUARE
* (square 4)
16
*
In the example, the REPL is started, and several Lisp expressions are evaluated. The result of adding 2 and 3 is printed as 5
, followed by the definition of a square
function. Finally, the square of the number 4 is calculated and returned as 16
.
Use case 2: Execute a Lisp script
Code:
sbcl --script path/to/script.lisp
Motivation: Executing a Lisp script allows you to run more complex Lisp programs that are stored in a separate file. This is useful when you have a larger program that requires multiple functions, definitions, or external dependencies.
Explanation:
To execute a Lisp script, you pass the --script
argument to the sbcl
command, followed by the path to the script file. The script file should have the .lisp
extension and contain valid Lisp code that you want to run.
Example output:
Assuming the script file hello.lisp
contains the following code:
(format t "Hello, World!")
Running the command sbcl --script hello.lisp
would produce the following output:
Hello, World!
In this example, the script file hello.lisp
is executed using the sbcl
command with the --script
argument. The Lisp expression (format t "Hello, World!")
is evaluated and the string “Hello, World!” is printed to the console.
Conclusion:
The sbcl
command provides a versatile interface for working with Common Lisp. By using the command to start a REPL or execute Lisp scripts, you can easily test and run Lisp code for various purposes, such as interactive development, prototyping, or running standalone applications.