An Example Procedure



next up previous
Next: Closures Up: Scheme Previous: Scheme

An Example Procedure

Let us examine a small Scheme procedure to get a feel for the language. The procedure in figure 1 splits a string of characters into a list of strings based on a delimiter character.

  
Figure 1: A sample Scheme function

In this example we have a top-level definition of the split-string function and an internal definition of the collect function. An internal, or nested function like collect has access to all lexical variables in the scope of its definition. More specifically, collect has access to the lexical variables string, delimiter and len. The let special form establishes a series of local variables and initial bindings. In split-string the let establishes only the binding of len. The cond special form evaluates the test in each of its clauses, performing the clauses action(s) when it finds the first test to succeed. The else clause of a cond form is executed if no other clause succeeds. Scheme also has other standard control constructs like if and case.

> (split-string "brent:WgG6SfAUnX5lQ:5359:100:Brent Benson" #\:)
("brent" "WgG6SfAUnX5lQ" "5359" "100" "Brent Benson")
>



Brent Benson
Mon Sep 19 16:03:14 EDT 1994