> Next Variables, or not
< Previous Introduction

5.8.2.1 Lists

Guile has several built-in primitives: characters, numbers (intrinsically exact integers or inexact floating-point values), strings, symbols, procedures, booleans, a null object, and most importantly, lists.

Ultimately, everything in Guile is a list. A Guile program is a list of procedure definitions; a procedure is a list of calls to other procedures; a call is a list whose first argument is the procedure to call, and the rest of the list is the set of arguments to pass into the procedure. All procedures return exactly one value, though this may be a (guess what?) list.

Literally, lists are lists of things separated by spaces and enclosed in parentheses.

Your first program might therefore look like

(display "hello world")

(Try it in the REPL). This is simply a call of a procedure bound to the symbol ‘display’, which takes a single argument; that argument is the second item in the list and is a literal string. The operation of the called procedure is to put the string onto the screen.

A little more:

(begin (display "Hello world") (newline))

(Try it!) Here three procedures (all provided as part of a standard Guile installation) are called. The procedure bound to the symbol ‘begin’ takes as many arguments as you want and executes each of them in order. The first procedure ‘begin’ executes is ’display’, which we’ve already seen. The second procedure ‘begin’ executes is bound to the symbol ‘newline’ — this is a procedure which takes no arguments and its effect is to move the screen cursor to the beginning of a new line.

Want to do some sums?

(+ 2 3)

will give the answer 5. This invokes a procedure bound to the symbol ‘+’ which takes as many arguments as you like, and the effect is to return the sum of them all. Bare numbers like ‘2’ and ‘3’ are interpreted as literal integer values (obviously!)

From a purely mechanical perspective, you now know everything there is to know about Guile! However, as with all languages, proper use only comes with knowledge of how the language implies semantics, and of the many procedures that come pre-packaged and which allow you to do useful work in practice. The next three sections talk about some of the semantic subtleties of the language.

To start with, a funny thing about lists themselves. There are two built-in procedures for accessing the elements of a list. The procedure bound to the symbol ‘car’ returns the first thing on the list. The procedure bound to the symbol ‘cdr’ returns everything else in the list, as another list !

(car (cdr ’(1 2 3)))

You may need to think about this for a while, but the result of the expression above is 2. Try it! Note the quote in front of the literal list; this stops Guile from trying to execute some procedure bound to the symbol ‘1’, but instead is the way that literal lists are written into Guile programs. Note further that with syntactic sugar the above code can be written

(cadr ’(1 2 3))

Because a Guile program is a list, and a Guile program spends most of its life manipulating lists, and a Guile program is translated on the fly, it is quite possible for a Guile program to manipulate its own code! This is simultaneously the power and beauty of the language, is the reason it has been used so extensively for the implementation of artificial intelligence and self-learning, self-tuning softwares, and computer algebra systems.

Here, we do not need such ferocious power, but leverage the simplicity of Guile programming (once you’re used to it) to control our robot at various levels of sophistication.

> Next Variables, or not
< Previous Introduction
Copyright © 2010, 2012 DM Bespoke Computer Solutions Ltd
All rights reserved