Imperative programming in Scheme |
This material is about functional programming in Scheme. So why do we include this section about imperative aspects of Scheme and LAML?
The reason is that Scheme in reality is a multi-paradigm programming language - although with strong historical roots in the functional paradigm. As a related observation, in Chapter 29 we saw that the distance between concepts in Scheme and the concepts in object-oriented programming is little too.
When Scheme is used for real world applications - like in the web domain - it is not possible to avoid use of imperative features. Just take, as an example, the handling of files on the harddisk. Some people may be able to regard file handling - deletion and file writing for instance - in a functional way. I prefer to think of these aspects as belonging to the well-known imperative paradigm.
In this brief side track chapter we will review some of the more important imperative aspects of Scheme.
30.1 Imperative Scheme Constructs | 30.3 String mutators |
30.2 List mutators | 30.4 Imperative features in LAML |
30.1. Imperative Scheme Constructs
Contents Up Previous Next Slide Subject index Program index Exercise index
We start by enumerating the main commands - and groups of commands - of the Scheme language. Be sure to notice the assignment syntactical form set!, which is the most important of them all.
The most fundamental imperative Scheme construct is the assignment set! |
|
As a notational convention, most imperative abstractions in Scheme ends with "!" |
As noticed above it is hard to avoid using imperative constructs when writing real-world Scheme programs in the web domain. But we should be careful. We do not want to mix traditional use of 'the imperative programming style' with functional programming. Thus, we should clearly avoid using set! side by side with all the functional means, which we have described in this material.
As a good rule of thumb, the imperative constructs in our programs should be kept at a minimum, and - most important - they should be used at a few places - typically at top level - such that the inner parts of the program are purely functional.
30.2. List mutators
Contents Up Previous Next Slide Subject index Program index Exercise index
In Chapter 6 we have studied the list concepts in Scheme (proper and improper lists) and we have seen the functional primitives for list construction and list selection - cons, car, cdr and the functions on top of these.
In this section we will mention and list the mutating functions, which are also available in Scheme.
Besides the list constructor cons and the list selectors car and cdr there are also
list mutators called set-car! and set-cdr! |
|
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 |
30.3. String mutators
Contents Up Previous Next Slide Subject index Program index Exercise index
Strings can be mutated in Scheme. Below we mention the main procedure for this, string-set!, and we also look at a similar procedure, string-fill!.
A string can be mutated by the string-set! and the string-fill! procedures |
|
There are similar functions that mutate the elements in vectors |
30.4. Imperative features in LAML
Contents Up Previous Next Slide Subject index Program index Exercise index
As mentioned in the introduction to this chapter, we need imperative features to deal with the real-world needs in both static and more dynamic web authoring and programming.
In this section we enumerate some of the more important imperative aspects in the LAML software package.
LAML needs imperative features for file IO, handling of LAML context information, and high level commands |
|
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. |