Troubleshooters.Com and Code Corner Present

Steve's Scheme Society
(With Guile)

Copyright (C) 2009 by Steve Litt
Note: All materials in Steve's Scheme Society are provided AS IS. By reading the materials in Steve's Scheme Society you are agreeing to assume all risks involved in the use of the materials, and you are agreeing to absolve the authors, owners, and anyone else involved with Steve's Scheme Society of any responsibility for the outcome of any use of these materials, even in the case of errors and/or omissions in the materials. If you do not agree to this, you must not read these materials.
To the 99.9% of you honest readers who take responsibility for your own actions, I'm truly sorry it is necessary to subject all readers to the above disclaimer.


       Content, this page                 Offpage Links

About Scheme

Scheme is a programming language. Scheme's statements are postfix. That means every statement looks like this:
(operator operand operand operand)
The number of operands is defined by the operator. Here are just a few examples:

0 operands
  • newline
  • (newline)
1 operand
  • display
  • (display   "Hello world\n")
2 operands
  • set!
  • (set!   myvar   10)
Indefinite number
of operands
  • +
  • -
  • (+ 20 4 3 2 1)
  • (- 20 4 3 2 1)

Most statements returns some sort of value. For instance, the + operator returns the sum of all numbers following it.

You can easily make your own operators and in so doing define how many operands they take.

Nested Statements

Consider this statement:
(display (+ 3 4))
The first (innermost) statement adds three and four, and the result is fed to the display operator, so 7 is written on the screen. Here's another example:
(* 5 (+ 3 4))
The innermost statement evaluates to 7, and its return value is multipled by 5 to produce 35, although it's not written because there's no display statement. The following is a formula for a hypotinuse, given the lengths of the two sides adjacent to the right angle:
(sqrt (+ (* adj1 adj1) (* adj2 adj2)))
There are two innermost statements, each of which squares one of the adjacent sides. The next statement out adds those two squares, and the statement outside of that takes the square root.

Other Ways of Explaining Scheme

Operators are pretty much equivalent to functions, procedures or subroutines in other languages, and the operands are equivalent to arguments to those functions. The value of a statement is just like the return value of a function in other languages.

Line Format

The following are all equivalent:
(display (* (+ 3 4) (+ 5 6)))
 
(display 
(*
(+
3
4
)
(+
5
6
)
)
)
 
(display 
(*
(+
3
4)
(+
5
6)))
digitkkkkkkkkkzzzzzzzzzzzzzzzzzz
Scheme cares nothing about lines -- it's all about the parentheses. There is one exception: An unquoted, unescaped semicolon is used to indicate that the rest of the line is a comment.

Comments

There are two types of comments:
  1. Semicolons are used to comment out the remainder of the line.
  2. Multiline or partial line comments are implemented by #! at the beginning and !# at the end of the comment.

Functional Programming

Functional programming is yet another programming paradigm. More than languages like C, Java, Perl, C++ and the like, Scheme is compatible with functional programming.

The idea of functional programming is that for a given function, its behavior depends only on its arguments, its effects are available only through the function return, and state has no place in the computations. The following hypotinuse formula is completely consistent with functional programming:
(sqrt (+ (* adj1 adj1) (* adj2 adj2)))
The return values of the multiplication operations depend only on their operands. The only effect of the multiplication operations is the return value. The sum operator takes as arguments the returns of the multiplications, depending only on them, and its only effect is its return, which is used by sqrt. All the way up the chain, every operation's action is defined exclusively by its arguments (operands), and the sole effect of each operation is its return value. If there were a variable elsewhere in the program, that variable could not possibly influence the hypotinuse formula. Therefore the formula doesn't depend on state.

Notice that this is the diametric opposite of OOP, where state is everything, and functions are mere methods whose purpose is to change or express the state of the object.

When it can be used, stateless programming is often simpler and more understandable. We all know how nasty break logic gets, or worse, state machines. When that stuff is necessary it must be used, but sometimes everything can be accomplished by simple functions, especially when you're free to do recursion.

Recursion is sort of an end-run around the "no-state" rule. Because each recursive instance keeps its own argument values, it does keep some state, although looking at it from a purely design viewpoint it's not dependent on state.

The "no state" rule isn't practical, but when programming Scheme, endeavor to use as few variables and state information as possible, making use instead of function arguments and returns.

Scheme is Doggedly Consistent and Easy to Remember

Of all the languages I ever learned, Scheme is the most consistent and therefore the easiest to remember. Every statement is an operator and its operands immediately surrounded by parentheses. Most statements return a value, and therefore can be used anywhere a value can be used, like as an operand in another statements. Thus statements are nested, with the outer ones calling more inner ones.

Guile

Scheme is a language, and Guile is a Scheme interpreter that comes with most Linux distributions. Guile is authored and maintained by the GNU team. Guyle also has special modules so it can be used as a front end to C programs designed with libraries interfacing to Guile. Through Guile you can use Scheme as an extension language for your applications. The C program can start Guile, from which Scheme commands can be read interactively or through a file.

Troubleshooters.ComCode Corner * Linux Library