Getting started with Chez Scheme and Emacs on Ubuntu
I’ve been an enthusiastic Mac user for about 12 years, but hardware problems with a recent MacBook Pro and friction surrounding the Catalina upgrade pushed me to evaluate other Unix-like systems. I pulled out an old ASUS laptop that originally had Windows 7(?) installed, but was most recently running CloudReady. I first tried installing FreeBSD because it seemed like an intriguing alternative, but the installation failed on the old hardware. I then tried Debian, but also failed. Finally, I reached for Ubuntu and, true to its reputation as being beginner friendly, was able to successfully complete the installation.1
I have been learning Chez Scheme over the past 6 months and had previously written about getting started with Chez Scheme and Emacs on macOS and Windows. For this post, I’ve copied the text of the previous post and updated only the components that needed to be changed to work with Ubuntu.
I installed Chez with APT.
$ sudo apt install chezscheme
Test the REPL with simple expression.
> (+ 100 10 1) 111
The REPL has several nice features including:
- Navigate through previous expressions with the up and down arrow keys.
- Autocomplete functions and paths with TAB.
- Write and edit multi-line expressions.
> (define (example x y z) (if (> x 0) (+ y z) (- y z))) > (example 1 2 3) 5
When navigating through previous expressions, only the first line of a multi-line expression is shown. To see (and edit) all lines, type CTRL+L. In the middle of an expression, RET creates a new line; to enter an expression from the middle of an expression, use CTRL+J.
Chez does not come with a package manager, but there are 3rd-party options, e.g., Akku. In this post, though, I will describe manual package management.
library-directories returns the directories where Chez looks for libraries.
> (library-directories) (("." . "."))
"." indicates that Chez is looking in the current directory.3 If you are using a project-based workflow, then you could include your dependencies in the current directory, perhaps in a
lib folder. For a ‘global’ approach, I created a library directory at
Before we go over where to stash that directory information, let’s cover library extensions.
> (library-extensions) ((".chezscheme.sls" . ".chezscheme.so") (".ss" . ".so") (".sls" . ".so") (".scm" . ".so") (".sch" . ".so"))
These are the file extensions that Chez uses when searching the library directories.
.profile to add information on library directories and extensions. From a Terminal window, open
.profile with the gedit text editor.
$ gedit ~/.profile
These lines add a new directory to
library-directories and a new extension to
export CHEZSCHEMELIBDIRS="/home/username/chez-lib:" export CHEZSCHEMELIBEXTS=".sc::.so:"
: at the end is used to indicate that the new entries should be appended to the existing entries. Remove the
: to replace the default values with the new entries. After saving
.profile, you need to logout from your user account and login again to make the changes to
.profile permanent. For your current session, though, you can source
$ source ~/.profile
Now, from a Chez REPL, we can see the effect of our changes.
> (library-directories) (("/home/username/chez-lib" . "/home/username/chez-lib") ("." . ".")) > (library-extensions) ((".sc" . ".so") (".chezscheme.sls" . ".chezscheme.so") (".ss" . ".so") (".sls" . ".so") (".scm" . ".so") (".sch" . ".so"))
If we have a library at
home/username/chez-lib/srfi/s1/lists.sls, then we import the library with
(import (srfi s1 lists)), i.e., you pass the components of the path to import. If you can’t import the library, look at the
library call at the top of
lists.sls, for example, because that will give you a clue of where the library expects to be placed in
> (xcons 1 2) Exception: variable xcons is not bound Type (debug) to enter the debugger. > (import (srfi s1 lists)) > (xcons 1 2) (2 . 1)
I’m not aware of an IDE for Chez Scheme, but pairing a good text editor with the Chez REPL provides a decent development environment. I chose Emacs as a text editor. I’m slowly becoming more comfortable with Emacs, but I’m far from proficient.
Emacs is available through the Ubuntu Software Manager.
The power of Emacs is in the keyboard shortcuts and customization. I’m too early in my journey to have unlocked much of that potential. When you are browsing info on Emacs, you will see shorthand for referring to keyboard combinations, e.g.,
C-f corresponds to CTRL+X followed by CTRL+F. The other important key is the meta key with
M as the shorthand. The default meta key is ALT. Similar to
.profile, Emacs can be customized through commands saved in the
Open Emacs, enter
C-f to find a file, and type
.emacs at the prompt. I added the following to
(require 'package) ;;; either the stable version: (add-to-list 'package-archives ;; choose either the stable or the latest git version: '("melpa-stable" . "https://stable.melpa.org/packages/")) ;; '("melpa-unstable" . "https://melpa.org/packages/")) (package-initialize)
.emacs and restart Emacs. Then type
M-x followed by
package-refresh-contents. If that is successful, you will see the message
Package refresh done in the minibuffer. To install Geiser, type
M-x and then
package-install. In response to the
Install package: prompt, type
geiser and hit return.
To customize Geiser, I used the menu options rather than directly editing the
.emacs file. Choose
Options/Customize Emacs/Specific Group... and type
geiser at the prompt. Click on
Geiser Implementation and change the default implementation to
Apply and Save. Restart Emacs.
The Chez REPL is launched through Emacs with
M-x followed by
run-chez. You can navigate through the previous expressions with ESC+P and ESC+N. Multi-line expressions, autocomplete, and syntax highlighting are also supported.
In Emacs, there is an option to highlight matching parantheses, which I find very helpful. Select
Options/Highlight Matching Parantheses and then
Options/Save Options. I’ve also started using company-mode for text completion. I was also pleased to discover that reindenting lines in Emacs is as simple as selecting the section to indent and pressing TAB.
Add the following lines to your
.emacs file for
scheme-mode to recognize the
.sls file extensions that are used with scheme code.
(add-to-list 'auto-mode-alist '("\\.sls\\'" . scheme-mode) '("\\.sc\\'" . scheme-mode))
In addition to using TAB to reindent lines in Emacs, my other most used keyboard shortcuts are for executing, commenting, and selecting code. To execute the code in an s-expression, place your cursor at the end of the s-expression and type
C-e.5 If the executed code displays any output, it will be shown in the minibuffer and not the REPL.6 To evaluate several s-expressions, highlight the region and type
C-r. To select an s-expression, place your cursor at the beginning of the s-expression and type
M-C-space (where space is the space bar). I’ve done a lot of fumbling around trying to select s-expressions by dragging the cursor with the mouse so I was excited to discover this last one.
During installation Ubuntu identified the apparent problem as related to numerous partitions from previous operating systems and provided the option to completely erase the old operating systems before installing Ubuntu. I’m sure that would also have been possible with FreeBSD and Debian, but Ubuntu made it easier to diagnose and address.↩︎
You can find the current directory by with
More complicated file structures for customizing Emacs are possible, but my proficiency with Emacs is not at that level, yet.↩︎
If you are receiving the message, “No Geiser REPL for this buffer”, then Geiser is struggling to figure out which Scheme implementation to use. One solution is to delete all of the non-Chez implementations by choosing
Options/Customize Emacs/Specific Group..., typing
geiserat the prompt, selecting
Geiser Implementation, and deleting all the non-Chez implementations under
Geiser Active Implementations. Click
Apply and Save. Restart Emacs.↩︎
If you want the output of the code displayed in the REPL, you will have to copy and paste it to the REPL (AFAIK).↩︎