# Class 35: Variable-arity procedures

Back to Tail recursion. On to Currying and uncurrying.

Held Tuesday, October 31, 2000

Summary

Today we visit variable-arity procedures, procedures that can take differing numbers of arguments.

Notes

• Today's stuff:
• Are there questions on exam 2?
• In case you hadn't noticed, I've started marking the preconditions for each procedure and I expect you to do the same.
• If the procedure explicitly checks the precondition, mark it as `[Verified]` or `[Checked]`.
• If the procedure does not explicitly check the precondition, mark it as `[Unverified]` or `[Unchecked]`
• I've also tried to make it clear that there are many different things you might say about postconditions (and perhaps you should make it a\ point to document default postconditions).
• Make it a practice to document your procedures with the six Ps
• Procedure name
• Paramters
• Purpose
• Produces (return value)
• Preconditions
• Postconditions
• I've noticed that many of you print the labs and readings. Next semester (when I've actually managed to put all the stuff together), should I simply hand out printed copies to conserve paper (I'll print two-up and double sided)?
• Happy Halloween!

Overview

• Definition: arity, the number of parameters a procedure expects.
• Some examples:
• `cons`
• `car`
• `expt`
• `list`
• `quote`
• `map`
• As you may have noted, some procedures do not have a fixed number of parameters. We call such procedures variable-arity procedures.
• Can you write your own? Of course.
• Two basic forms.
• First form: 0-or-more parameters:
```(lambda args
body)
```
• `args` is a list.
• Example
```> (define first
(lambda stuff (car stuff)))
> (first 1 2 3)
1
> (first 3 4 5 6 7)
3
> (first 3)
3
> (first)
car: expects argument of type <pair<; given ()
```
• Observation: Sometimes you want to require parameters.
• Second form: 1-or-more parameters
```(lambda (arg1 . args)
body)
```
• `arg1` is a single value; `args` is a list.
• Example:
```*gt; (define first
(lambda (arg . args)
arg))
> (first 1 2 3)
1
> (first 3 4 5 6 7)
3
> (first 3)
3
> (first)
procedure first: expects at least 1 argument, given 0
```
• Variations: 2-or-more-parameters
```(lambda (arg1 arg2 . args)
body)
```

## History

Thursday, 24 August 2000

• Created as a blank outline.

Back to Tail recursion. On to Currying and uncurrying.

Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.