Chapter 9
Imperative programming in Scheme

Kurt NÝrmark
Department of Computer Science, Aalborg University, Denmark

Previous lecture
Index References Contents
Although we use Scheme for functional programming, Scheme can be used for imperative programming as well. In this side track we will take a look at this aspect of the language.

Imperative Scheme constructs

Imperative Scheme Constructs
Slide Annotated slide Contents Index
References Textbook 

The most fundamental imperative Scheme construct is the assignment set!

  • Other imperative constructs:

    • (begin e1 ... en)

    • The iterative do control structure

    • The input output procedures

    • The list, string and vector mutators

As a notational convention, most imperative abstractions in Scheme ends with "!"


List mutators
Slide Annotated slide Contents Index
References Textbook 

Besides the list constructor cons and the list selectors car and cdr there are also list mutators called set-car! and set-cdr!

  • The list mutator procedures:

    • The command (set-car! x y) changes the value of the car position of the cons cell referred by x. The car position is assigned to y

    • The command (set-cdr! x y) changes the value of the cdr position of the cons cell referred by x. The cdr position is assigned to y

    • Using the list mutators it is possible to make circular structures

Without use of the list mutators, and with use of structural equivalence predicates, it is not possible to tell the difference between a list structure and a copy of the list structure

If we make use of list mutators we can tell the difference between a list structure, LS, and a copy of LS. The way to do it is to mutate LS and observe that the copy is not changed. Using only the functional subset of Scheme (and disregarding the very discriminating equality predicates such as eq? and eqv?) a copy of LS serves the same purposes as LS itself.


String mutators
Slide Annotated slide Contents Index
References Textbook 

A string can be mutated by the string-set! and the string-fill! procedures

  • The string mutator procedures:

    • (string-set! str k chr) changes character number k in str to chr

    • (string-fill! str chr) changes every character in str to chr

There are similar functions that mutate the elements in vectors


Imperative features in LAML

Imperative features in LAML
Slide Annotated slide Contents Index
References Textbook 

LAML needs imperative features for file IO, handling of LAML context information, and high level commands

  • Overview of imperative features in LAML:

    • The procedures write-text-file and read-text-file

      • At a higher level procedures such as write-html which surrounds many HTML mirror documents in Scheme

    • Many high level procedures which represent tools or commands

      • Such as xml-parse, xml-pp, and schemedoc

      • File handling in general

    • Definition of LAML context information

      • fake-startup-parameters, laml-cd, ...

In some situations we have internally in LAML used imperative patching of functional programs, because functional patching has been too difficult and too time consuming

Unfortunately, we have not systematically used the 'exclamation mark' naming convention of LAML procedures.


Collected references
Contents Index
Input output procedures
Scheme assignment
List and pairs in Scheme
R5RS: Vectors in Scheme
R5RS: Strings Scheme
LAML tools and commands
The Text Collection and Skipping Library
Reading of writing of text files


Chapter 9: Imperative programming in Scheme
Course home     Author home     About producing this web     Previous lecture (top)     Next lecture (top)     Previous lecture (bund)     Next lecture (bund)     
Generated: July 2, 2013, 09:16:16