Pragmattica

November 25, 2007

Getting Started with Scheme on Ubuntu — Part 2

Filed under: Linux,Scheme — bnsmith @ 11:59 pm

Installing and Using SLIB

Scheme is a very minimalistic language. This is Scheme’s great strength, but this trait can also make the language seem a little bit barren. There is a collection of excellent libraries that can help allieviate this, though using them requires some set-up. First, follow the directions from Part 1, if you haven’t already. As with Gambit Scheme itself, the version of SLIB in the Ubuntu repositories is not the latest, so we will install from source.

Go to the SLIB website and download slib3a5.zip to your home directory. The download link is under the “Quick Start” heading.
pic0.png

Next, open a terminal (Applications -> Accessories -> Terminal) and enter the following commands:

unzip slib3a5.zip
cd slib
sudo make install

Then, edit the SLIB configuration file with the command:

sudo gedit /usr/local/lib/slib/gambit.init

Find the line:

  ((unix)    (lambda () "/usr/local/share/gambc/"))

pic12.png

and change it to:

  ((unix)    (lambda () "/usr/local/Gambit-C/current/"))

pic22.png

Now start the interactive interpreter with root permissions:

sudo gsi -:s /usr/local/lib/slib/gambit.init -

In the interpreter, enter the command:

(require 'new-catalog)

Exit the interpreter by pressing CTRL-D twice. Finally, copy and paste this command into your terminal:

echo "(include \"/usr/local/lib/slib/gambit.init\")" > ~/.gambcini

SLIB is now fully installed and can be used by the Gambit Scheme interpreter. To test the installation, run the interpreter by typing gsi and paste in the following code:

(require 'random)
(random 10)

This should display a random number from zero to nine. It is similarly possible to use SLIB with programs saved in files. Copy the following code into a text file named slibtest.scm:

(require 'printf)
(require 'srfi-1)
(define userlist '((45607 "Ted Randalph" 34.90 5)
                   (91238 "Rob Smith" 32.45 3)))
(for-each
  (lambda (userinfo)
    (printf "Employee %s (%d) has %d years experience and makes $%.2f per hour.\n"
      (second userinfo) (first userinfo) (fourth userinfo) (third userinfo))) userlist)

This program can be run by entering gsi slibtest.scm and should display the following output:
pic5.png
Check out the SLIB documentation for a complete list of what is available.

Advertisements

Blog at WordPress.com.

%d bloggers like this: