Programming in Scheme — The Quick and Definitive Scheme Tutorial : Part One

Arun Muthu
Atomic Variables
Published in
5 min readJan 7, 2020

--

Scheme Programming Tutorial
Scheme Programming r5rs : The Revised5 Report on the Algorithmic Language Scheme

Hello Internet! This is Arun and welcome to part one of the Scheme Programming Tutorial. This blog is intended for programming beginners as well as experienced ones in other languages who would like to get up to speed with Scheme.

First things first …

What is Scheme?

Scheme is a language that is a variation of the Lisp programming language that was created at MIT’s Artificial Intelligence lab by Guy Steele and Gerry Sussman in the year 1975.

Why do I need to learn Scheme?

Well.. probably you are a student and you have an exam coming up 😜 or you need to learn it for something coming up workplace. Or you might need it for something else and hence you discovered this post !!. Okay, back to the question.. , there are two major reasons why; Scheme places an emphasis on Functional programming and Recursive Implementation. This will enable one to be a better programmer and will definitely improve the skill of identifying sub problems. I personally learned Scheme from my Programming Languages class at NYU taught by an awesome professor.

Installation:

In this tutorial we will be using the The Revised5 Report on the Algorithmic Language Scheme, which is a popular flavor of Scheme. To run and test our Scheme programs we will be needing an IDE (integrated development environment) called Dr. Racket. The installation link can be found here. Please select your suitable OS for the installer package. Installation must be fairly straightforward. After installation, the language R5RS Scheme must be selected. (DrRacket supports a lot of dialects). To choose it, click on the drop-down menu at the bottom of the Dr. Racket window and the Choose Language option can be seen. From there do : >> Other Languages >> R5RS and then press OK. This should setup R5RS as one of the languages. If you now have a white screen that looks something like this with R5RS at the bottom, you are good to go!

The DrRacket IDE

Lets get started with Scheme Programming. Here we go !

For now, we will be using the bottom part of the IDE, or the interpreter. The interpreter instantly outputs the result of our query (command) one by one as opposed to the top part which does it all at once. Scheme uses a lot of brackets, and I mean a lot.

A quick note : This tutorial will mainly focus on learning with examples as opposed to text books and other material online which drone on and on about the different types of syntaxes and data types etc .. Once you start working with on hand examples, the language can be grasped quickly. This material is intended to get you writing functions of your own as soon as possible, for more specific language and syntax related queries, visit scheme.com for a complete documentation.

1: Using Scheme to do basic calculations

Any function in scheme must be enclosed by a pair of braces ()

Lets add two number, 1 and 2. 1+2 is written as (+ 1 2) in scheme. Make note of position the brackets and the prefix notation style where the 1 and 2 are parameters of the operator ( the symbol ‘+’ here ). The command follows the ‘>’ symbol. Outputs are in bold. Feel free to try it out in the Racket interpreter.

> 33 
33
> (+ 1 2)
3
> (* 3 5)
15
> (/ 3 2)
1 1/2
> (exact->inexact (/ 3 2))
1.5
> (/ 100 5 2)
10
;Anything following a ';' is a comment;For more complex calculations, brackets can be nested.> (* (+ 2 5) (- 4 1))
21
> (quotient 4 3)
1
> (remainder 13 9)
4
> (modulo 13 9)
4
> (sqrt 16)
4

The basic types in scheme are integers, floating point numbers, strings, booleans(#t (true), #f (false)), and symbols. An example of a symbol is ‘a where a is a datatype which does not carry any info other than the name a.

> 'a
a

2: The concept of Cons

Let’s see an example to see how the cons function works.

> (cons 1  2)
(1 . 2)

The cons function creates a memory space which contain two storage addresses. It is something like this in simple words : two items combined with each other.

simple cons cell
Example of a simple cons cell
> (cons 4 (cons 1 2))
(4 1 . 2)

Cons’es can be nested, the actual output for the above statement is (4 . (1 .2)) which is written as (4 1 . 2) in short. The memory representation of the above is .

nested cons cell
Nested/Beaded cons cell

3.Lists

Scheme does not have C or Java type arrays; lists are the primary aggregate types. A list can be created using the following command :

> (list 3 4 5 6)
(3 4 5 6)

A list is actually a set of beaded cons cells with the last element as ‘() which denotes an empty list.

So to keep each article short and crisp, I’m going to end tutorial one here so that each article is easily digestable. The next article will focus on how to manipulate lists and perform basic functions. The third article will feature tougher examples of lists related programs.

See you in the next post

Goodbye Internet!!

--

--

Arun Muthu
Atomic Variables

Computer Nerd | Oracle | MS CS @ The Courant Institute of Mathematical Sciences, New York | Views are my own