Scheme Programming/Looping

Simple Recursion
Scheme is very odd in one sense, it has no expressions designed for looping, repeating or otherwise doing something more than once at a general level. The only easy way to do this is recursion, that is, designing a procedure such that it meets 2 criteria:


 * 1) The procedure must have a base case that it stops at
 * 2) The procedure must call itself, possibly with modified arguments.

The most simple recursive procedures to write are tail recursive procedures. Lets think about how to compute the factorial of an integer. The factorial of an integer is that integer, multiplied by every integer below it.

$$ n! = n \cdot (n - 1) \cdot (n - 2) \cdots 1 $$

Now with a little bit of ingenious observation, we notice something special about this function:

$$ n! = n \cdot (n-1)! $$

Now all we need to do is have a base case. 0! is 1 (the mathematical explanation of this is beyond the scope of this book). Now that we have a relationship between a factorial and a factorial of a different (smaller) number, and a base case, we can quickly write a recursive definition of a factorial function:

Now all we have to do is test that it works.

As you can see, this appears to work. This shows the general form of the most basic recursive functions.

(define  (lambda () (if ()       ( ( ))))

However, this leads to an inefficient method of computing the factorial of an integer, as Scheme must keep track of all of the intermediate variables. For example, lets look at the execution flow of

Notice how this expands and contracts again, and that each intermediary value is stored. This is very inefficient with regards to the amount of space needed. Imagine trying to find the factorial of 1000. You'd need 1000 little notes of where you'd been so far. Unless there is no other way, this is generally discouraged.

Let's try implementing a slightly more abstract way of doing this. Think about summing a series of values together:

$$\sum_{n = Lower}^{Upper} f(n)$$

Which translates to:

$$f(Lower) + f(Lower + 1) + \cdots + f(Upper) $$

This can also be translated into a Scheme procedure

Once again, we can test this;

Which also exhibits similar flaws to the previous factorial procedure: It takes up a lot of 'stack space'.

The 'do' form
There is also an iterative construct, do, which can simplify writing some functions. For example, this procedure sums all the elements of the given list:

Here, the local variables remaining and final-sum are given the initial values of lst and 0, respectively. Then the test case, here (null? remaining) is evaluated. If the test succeeds, then the following expression is evaluated and becomes the final value of the entire do form. If it fails, Scheme evaluates the display and newline lines for their side effects, then redefines remaining and final-sum with the values (cdr remaining) and (+ final-sum (car remaining)) respectively. We can generalize this as follows:

This first creates the variables var1, var2, ... and gives them the initial values base1, base2, ... Then the test case (test? ...) is evaluated, and if it succeeds, the function evaluates and returns final-exp. If the test fails, side-effecting-statements ... are executed, var1, var2, ... are redefined with the new values from exp1, exp2, ... and then the test case is evaluated again. Failing to provide a proper test case could potentially lead to either an error in Scheme or an infinite loop.

map and for-each
Scheme defines two functions to iterate over a list. map and for-each both iterate over every element of a list and call a procedure with each element. Map stores the return values of each call to the function, while for-each does not:

For-each is in fact similar to the "foreach" loop found in many programming languages. In fact, we could define such a loop as new syntax, adding it to the language:

The resulting "foreach" form iterates over multiple lists in parallel:

Iterative recursion
We can use iterative recursion to reduce the space requirements of many procedures. We shall look at the factorial procedure once again. Rather than keeping a long list of all the values to be multiplied at a later date, we could keep an ever-changing answer, and pass it to the next call of our procedure. Let's look at how this works when applied to the factorial function:

Now we can check that it works:

As we can see, this both has an automatic advantage and disadvantage. We make it use less space, and we need to effectively specify the base case when it is invoked. We shall investigate the computational process it generates first.

We shall follow the computation of 5! through once again, but using the iterative procedure this time:

Notice how this one never expands and then contracts again, and there is nothing "left to later" (often referred to as "Deferred processing"), and this version of the procedure only keeps track of 2 variables, regardless of the size of its input.

Now we shall tackle the issue of the extra, unnecessary parameter; this requires a slight re-definition of the procedure,

This solves the problem by showcasing a previously un-introduced feature of Scheme. That is; functions may, internally, contain definitions of new functions and variables. These are not visible outside the procedure. This new procedure solves both of our previous problems with the first recursive factorial procedure. It operates in both constant space, and it is easy to use.

Now we shall show that sum can also be implemented in a very similar way:

An interesting side note is that both factorial and summation can be abstracted to a more generic procedure, that can be used for even more than factorials and sums. Allowing for products, numerical integration and other interesting methods to be implemented. I'll leave this as an exercise to the reader (it's not overly challenging, but if you've not done any programming before, or you don't have a mathematical background, this exercise can be challenging).

Looping Over Lists
Suppose we have a list, and we need to find if an element exists in that list. We would have to go through every element of that list, and check it against our desired result.

Notice how we first check that we aren't out of elements to check against, this prevents us trying to take the  of an empty list (thus causing an error). Then, we check to see if we have a match, Otherwise, carry on searching. Unless you have an infinite list (this is possible in Scheme, we'll see how later), this procedure will reach a definite answer.

We also may have the problem of adding two to every element of a list.

Here, we are 'mapping' each element of the list to some value. In this specific case, that value is the value of each element of the list, plus two. What if we wanted to multiply the value of every element of a list by 3? Or find the square root? We would have to write the code each time, and each would look very similar. It would be more reasonable to abstract what the particular function is and allow the programmer to substitute it in each time.

Now we can define plustwo more succinctly:

This shows a very important concept in Scheme. Function can be passed around just like numbers, strings, atoms, and other things. Using this idea, we can go on to address the problem of finding if an element is in a list as above.

We first notice that the output of the function can only be #t or #f, or some error. Ignoring the error case to begin with, we can begin to write a way of abstracting out the case of testing each individual element for some predicate. We can then use that function to find if any element is equal to any given value, using map.

We'd like to be able to "fold up" a list of such values into a single value: #t if any of the values are #t, or #f otherwise. We'll name our function foldr.

We can now use this folding function to define a function called any, which tells us if any of a list satisfy a predicate (a predicate being a function which returns #t or #f).

Using this function we can re-write exists-in? as a much simpler function.

Which can more easily be read as "Is any element of the list 'lis' equal to 'ele'?". It is also shorter, and given that foldr, map and any have been implemented correctly, easier to find any problems with it.

Folds are often used in functional programming to express solutions to a problem in a very succinct way, or to avoid re-writing the same code for defining a recursion repeatedly.