setup lisp programming environment

Lisp, the 55 year old language, is the best bet to learn the functional programming paradigms. Here is quick way to get started w/ coding in Lisp (actually Scheme, a dialect of the main language) in recent Debian/ Ubuntu systems:

1. Emacs
This GNU text editor is created using Lisp itself, and is the typical one to use for Lisp. It comes with all features one might need to edit Lisp code. To install the latest Emacs (24.3),

$ sudo add-apt-repository ppa:cassou/emacs
$ sudo apt-get update
$ sudo apt-get install emacs24

2. Guile – Scheme interpreter
Up next, is the Lisp interpreter. Although Emacs comes with its own lisp (called Emacs Lisp), we’ll use Scheme, a simpler dialect of Lisp. We’ll also use Geiser, for the REPL. Scheme interpreters supported by Geiser are Guile and Racket, with the former being lighter to install.

$ sudo apt-get install guile-2.0

One may also use this interpreter from command line to load and run a Scheme script:

$ guile -s sript_file.scm

[Optional: Run Scheme from Sublime Text]
The above step automatically brings the idea of running Lisp from Sublime Text 3, our friendly text editor. Once the interpreter is installed, it is as simple as creating a new build system (Tools > Build System > New Build System). Modify the contents to:

{
"cmd": ["guile", "-s", "$file"]
}

and save as ‘~/.config/sublime-text-3/Packages/User/Scheme.sublime-build’. Now the simple “Tools > Build” (Ctrl + B) will compile and run the currently opened file, producing an output like:

[Output of Scheme script]
;;; note: auto-compilation is enabled, set GUILE_AUTO_COMPILE=0
;;; or pass the --no-auto-compile argument to disable.
;;; compiling ~/dev/scheme/ex.scm
;;; compiled ~/.cache/guile/ccache/2.0-LE-8-2.0/home/user/dev/scheme/ex.scm.go
[Finished in 0.1s]

3. Geiser – for the REPL
Functional programming languages are not complete without the REPL. It is the quickest way to test code on the fly. While Racket comes with its own REPL, for Guile we can go with Geiser. This is a lightweight extension for Emacs that provides a bunch of Emacs modes for ‘Scheme hacking’ within Emacs. For Emacs shortcuts used below, C=Ctrl, M=Alt keys.

Download the package file and install from within Emacs:

M-x package-install-file
Package file name: /path/to/downloaded/package/file.tar

4. Hello, Lisp!
We should see a new Menu for Geiser the moment we restart Emacs after step 3. To test our efforts so far, create a new Scheme file:

C-x C-f /path/to/new/file.scm

See #5 for why we used .scm as extension.

a. Add some code

(define (cube x) (* x x x))

and save using C-x C-s.

b. Compile the code using C-c C-k. It might complain that no Scheme interpreter is loaded. In that case, load the interpreter/ REPL first (step #5), then compile.

The shortcut commands are also available in ‘File’ and ‘Geiser’ menus respectively, but while in Rome …

5. Start Geiser REPL
Our Geiser REPL starts off by using a Lisp interpreter among the ones it expects to be installed (i.e. guile or racket). It can automatically select the interpreter based on the extension of source code. There are a bunch of extensions that can be used with Lisp:
.scm: Typical Scheme files (Geiser starts Guile)
.ss: Old school Lisp files (Geiser starts Racket)
.rkt: Racket files (Geiser starts Racket)

If it is not able to figure out the right interpreter, it will prompt for one of the two. We can select one by just tabbing out at the prompt. Another way of telling Geiser which interpreter to use is add this line to the beginning of source code:

;; -*- geiser-scheme-implementation: guile -*-

For more information, see here.

Start the REPL from within the source code buffer. It will open a new buffer.

C-c C-z
OR
M-x run-geiser

Now simply evaluate any expression defined in the source code:

REPL> (cube 3)
$1 = 27

To toggle between REPL and source code buffers, use C-c C-z. While toggling back to REPL, compile the modified script first.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s