Racket & Lisp
Why Lisp?
Once you learn Lisp you will see what is missing in most other languages.
– Richard Stallman
Within a couple weeks of learning Lisp I found programming in any other language unbearably constraining.
– Paul Graham
Lisp is a language for doing what you've been told is impossible.
– Kent Pitman
Lisp isn't a language, it's a building material.
– Alan Kay
Lisp is a programmable programming language.
– John Foderaro, CACM, September 1991
The best description of the Lisp programming language is that it is a high level machine language. That is, it shares many of the facets of contemporary machine language –the necessity for attention to detail and the freedom to manipulate the machine's data and programs without restriction– yet Lisp is high level in that the language contains the expressive power and convenience of traditional high level languages.
– John Allen
Racket
Racket is a general-purpose, multi-paradigm programming language based on the Scheme dialect of Lisp. It is designed as a platform for programming language design and implementation.
You can download the DrRacket programming environment on your laptop.
(define (factorial n) (if (zero? n) 1 (* n (factorial (- n 1)))))
sbcl
Steel Bank Common Lisp (SBCL) is a high performance Common Lisp compiler. There are instructions for downloading it and installing it on your laptop.
It is also installed on our Linux server.
sbcl from the command line
You can run it from the command line.
bon@jojo:~$ sbcl This is SBCL 2.1.1.debian, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>. SBCL is free software, provided as is, with absolutely no warranty. It is mostly in the public domain; some portions are provided under BSD-style licenses. See the CREDITS and COPYING files in the distribution for more information. *
The asterisk is the Lisp prompt.
You can exit sbcl by Control d.
Because it is sometimes convenient to be able to edit what you enter into
sbcl, you can wrap the sbcl command in Readline. Put the following lines in
your .bashrc or similar.
sbcl () { /bin/rlwrap sh -c "/usr/local/bin/sbcl --noinform $*" }
Now sbcl will start silently and you can use Emacs editing commands on what
you enter.
sbcl in Emacs
In Emacs we use SLIME to interact with Lisp. Your Emacs config file contains
the necessary configuration so you should be able to do M-x slime to get a
buffer with an sbcl prompt.
Quicklisp
Quicklisp is a library manager for Common Lisp. It plays a similar role in the Lisp ecosystem as pip does in the Python ecosystem. Using Quicklisp we can download, install and manage Lisp packages.
Once you have sbcl installed you can install Quicklisp as follows
From the terminal, download the installer
curl -O https://beta.quicklisp.org/quicklisp.lisp
Run the installer using sbcl
sbcl --load quicklisp.lisp
You should see something like
$ sbcl --load quicklisp.lisp This is SBCL 1.0.42.52, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>. SBCL is free software, provided as is, with absolutely no warranty. It is mostly in the public domain; some portions are provided under BSD-style licenses. See the CREDITS and COPYING files in the distribution for more information. ==== quicklisp quickstart loaded ==== To continue, evaluate: (quicklisp-quickstart:install) *At the sbcl prompt type or copy/paste. Note that the parentheses are important.
(quicklisp-quickstart:install)
Press the return key and see the installer download Quicklisp. The installation ends with
==== quicklisp installed ==== To load a system, use: (ql:quickload "system-name") To find systems, use: (ql:system-apropos "term") To load Quicklisp every time you start Lisp, use: (ql:add-to-init-file) For more information, see http://www.quicklisp.org/beta/ NIL *
Ask Quicklisp to install itself every time you start up sbcl. It will ask you to press enter to confirm.
(ql:add-to-init-file)
- Type
Control Dto leave sbcl. Check that Quicklisp is working by using it to installt the
alexandriapackage of useful Lisp utilities. You should see something like this.$ sbcl This is SBCL 2.1.1.debian, an implementation of ANSI Common Lisp. More information about SBCL is available at <http://www.sbcl.org/>. SBCL is free software, provided as is, with absolutely no warranty. It is mostly in the public domain; some portions are provided under BSD-style licenses. See the CREDITS and COPYING files in the distribution for more information. * (ql:quickload :alexandria) To load "alexandria": Load 1 ASDF system: asdf Install 1 Quicklisp release: alexandria ; Fetching #<URL "http://beta.quicklisp.org/archive/alexandria/2021-12-09/alexandria-20211209-git.tgz"> ; 54.51KB ================================================== 55,819 bytes in 0.00 seconds (0.00KB/sec) ; Loading "alexandria" [package alexandria].............................. [package alexandria-2]. (:ALEXANDRIA) *
.sbclrc
When you start sbcl it will run the Lisp code in the file ~/.sbclrc If you
open this file in Emacs you should now see the following Quicklisp
configuration.
;;; The following lines added by ql:add-to-init-file: #-quicklisp (let ((quicklisp-init (merge-pathnames #p"/home/bon/code/lisp/quicklisp/setup.lisp" (user-homedir-pathname)))) (when (probe-file quicklisp-init) (load quicklisp-init)))
(defun factorial (n) (if (zerop n) 1 (* n (factorial (- n 1)))))