CSC 153: Computer Science Fundamentals Grinnell College Spring, 2005 Laboratory Exercise Reading

# Variable Arity

## Abstract

This reading introduces the concept of procedures with varying numbers of parameters.

## A Running Example

To motivate our discussion and provide a running example, consider what capabilities we might want for printing several results. If we need to print three values (result1, result2, result3) on a line, one approach might utilize the display and newline procedures:

```
(display result1)
(display result2)
(display result3)
(newline)
```

However, this approach is somewhat cumbersome, as we must call display for each value with newline at the end. Of course, we could package such an operation in a procedure display-3, but then we also might want procedures display-2, display-4, display-5, etc.

Instead, it would be more convenient to have a display-line operation which would print as many values as we wish on the line; we would like to define display-line so that it can be used for 1 or 2 or 3 or more values, and all would be printed on a line -- with a newline at the end.

Such a procedure can be defined in several ways, taking advantage of alternative forms of lambda expressions.

### Approach 1

As a first approach, consider the following procedure:

```
(define display-line
(lambda values
(let loop ((lst values))
(if (null? lst)
(newline)
(begin
(display (car lst))
(loop (cdr lst))
)
)
)
)
)
```

A sample run follows:

```
> (display-line "To" "Be" "Or" "Not" 2 "B")
ToBeOrNot2B
```
In this procedure, note that the symbol values after lambda is not in parentheses. Scheme interprets this syntax to indicate that all parameters to the function should be grouped together in a list. Thus, the sample applies display-line to the list ("To" "Be" "Or" "Not" 2 "B").

### Approach 2

While the above code prints out all parameters, sometimes we might prefer to print out the items separated by a specified string. Thus, we might want to generalize display-line by giving, as its first parameter, the string to be used as a separator. Some examples might be:

```
>(display-line "" "going..." "going..." "gone")
going...going...gone
>(display-line "..." "going" "going" "gone")
going...going...gone
>(display-line (string #\tab) 1996 'foo 'wombat 'quux)
1996    foo     wombat  quux
```

In this case, display-line only makes sense if it contains a first parameter, but we cannot anticipate how many elements might appear after that. Another form of lambda expressions allows us to bind initial parameters to specific parameters, with any remaining parameters collected together in a list. This is illustrated in the following code:

```
(define display-line
(lambda (separator . element-list)
(let loop ((lst  element-list))
(cond ((null? lst) (newline))
((null? (cdr lst)) (display (car lst)) (newline))
(else
(display (car lst))
(display separator)
(loop (cdr lst)))
)
)
)
)
```

Here, (separator . element-list) follows lambda. In this context, the first parameter is bound to separator, while all remaining parameters are placed on a list and bound to element-list.

The dot notation can be used to specify any number of initial values. Thus,

```
(first-value second-value . remaining-values)
```

gives explicit bindings for two parameters, with any further parameters collected on a list for remaining-values.

This document is available on the World Wide Web as

```http://www.cs.grinnell.edu/~walker/courses/153.sp05/readings/reading-variable-arity.shtml
```