[0003] First steps in programming with Scheme
Scheme is a general-purpose programming language. It was designed to be a simple language that has the bare minimal amount of rules that are needed to have a fully functional programming language. With careful design it tries to have no restrictions on how computational expression can be composed. This way it can support most of the major programming paradigms and can easilly adopt new ones as well.
In this monograph I will show just a few aspects of Scheme.
Scheme comes with a large variety of basic data types like numbers, characters, boolean values and many more.
Numbers evaluate to themselves in Scheme:
123
output:
123
Integer, rational, real, and complex numbers are all supported.
Strings also evaluate to themselves:
"Hello World"
output:
"Hello World"
Another important data type is the list. A list can hold zero or more values and the values can be of different types. The list is represented by a pair of parentheses with between them the values each separated by spaces. There are a couple of ways to create a list, but the one of the simplest forms is by using a single quote in front of the list.
'(1 2 3)
output:
(1 2 3)
The list unquoted will not be evaluated to itself, but they form the basis of all computations in Scheme. The first value in a list is the will be used the operator and the other values as its operands. This way data can be code and vice versa and this gives Scheme and other Lisps a lot of expressive power. Lets have a look how this works by looking at the procedure +.
(+ 2 5)
output:
7
Here the evaluation of the list (+ 2 5) will be evaluated where the + will be the operator with 2 and 5 as its operands. The result of the operation is then returned. In this case 7.
When programming, its helpful to name values. By using names you give meaning to values. For example 39 could be the height of a tree in meters. Or the age of a person. Or the house number of a friend. So naming takes away the magic meaning of a value. In Scheme we can define an identifier that is assigned to a value. This is done by using the procedure 'define', it needs to be followed by an identifier and a value.
(define age 39) age
output:
39
The procedure 'define' can also be used to name procedures. A procedure is a set of expressions that can take input.
(define (square x) (* x x)) (square 4)
output:
16
Playing with values and procedures is a lot of fun, but for most programs making decisions based on the results of procedures is important. For example if you want to solve the problem of telling the user if a number is odd or not, you need to perform an action and the result determines what you need to tell the user. The procedure 'if' can be used to take two different set of expression and perform one of them based on the result of a value.
(define (even? x) (= (remainder x 2) 0)) (define (odd? x) (not (even? x) (define (tell-if-its-odd x) (if (odd? x) (display "It's odd") (display "It's not odd"))) (tell-if-its-odd 3)
output:
It's odd
Before I end this first taste of programming with Scheme, I still want to talk a litle bit more about lists. Scheme has many functions that work on a list, because its a very powerful structure. We could for example ask Scheme to tell if values in a list are odd or not:
(map odd? (list 1 2 3 4 5 6))
output:
(#t #f #t #f #t #f)
There is much more to say about Scheme, but I will stop here for now. More will come and I will use Scheme as the high level language for the monographs on programming. I hope you liked it so far.
Attribution
Scheme as been designed by Guy L. Steele Jr. [1] and Gerald Jay Sussman [2].
Further reading
Wikipedia has a nice entry on the language [3]. The standard R5RS [4] is the most implemented version of Scheme. But there is also the R6RS [5] and R7RS small [6].
Structure and Interpretation of Computer Programs (SICP) is a Computer Science textbook that uses Scheme [7]. The book doesn't explain Scheme. The authors expects that students will pickup the language naturally, because of its simple syntax. There is a textbook that is written explicitly as an introbook for SICP that does explain scheme [8]
How to run the code
Scheme has many implementations [9]. But all the examples given should run in all of them. They can be run in a REPL (Read-Eval-Print Loop) that is provided by almost all if not all implementations or loaded through a file.
Please read the documentation of the specific implementation you want to use for specifics.
References
[1] Article on Guy L. Steele Jr. on Wikipedia
[2] Article on Gerals Jay Sussman on Wikipedia
[3] Scheme article on Wikipedia