# Class 39: Variable-Arity Procedures

Reading: Variable-Arity Procedures. Lab: Variable-Arity Procedures. Back to Tail Recursion, Continued. On to Multiple-Valued Procedures.

Held Thursday, April 12, 2001

Summary

Today we consider how and why to write procedures that can take different numbers of arguments in different situations.

Notes

• For tomorrow, read Procedures that Return Multiple Values.
• Monday, April 16 at 4:15 p.m. in Noyce Science Center 2400, join artist Irve Dell, the Department of Mathematics and Computer Science, and the Faulconer Gallery to celebrate the installation of permanent art works in the Math/CS stairwell.
• Only today and tomorrow for questions on exam 2
• How do you write to a file? Here's some sample code.
```(let ((outport (open-output-file "example")))
(display "Hello There" outport)
(newline outport)
(display "Okay, now I'm done" outport)
(newline outport)
(close-output-port outport))
```
• Question 3: Currying
• I'd prefer if you wrote `cmember?` directly, rather than implementing it with a call to `member`.
• The name of `cmember?` suggests that it returns only `#t` or `#f`.

Overview

• Definition of arity
• Why have variable-arity procedures
• How to write variable-arity procedures
• Lab

## Variable-Arity Procedures

• Def. Arity: The number of parameters a procedure expects.
• Some procedures naturally take varying numbers of parameters:
• `+` takes 0 or more parameters.
• `display` takes 1 or 2 parameters.
• `map` takes 2 or more parameters.
• It would be nice to be able to write our own procedures of variable arity (that is, varying numbers of parameters).
• Zero or more parameters:
```(lambda params
body)
```
• `params` is not parenthesized.
• In your body, treat `params` as a list.
• One or more parameters:
```(lambda (first . remaining)
body)
+ In your body, treating `remaining` as a list.
```
• Two or more parameters:
```(lambda (first second . remaining)
body)
+ In your body, treating `remaining` as a list.
```

## History

Friday, 12 January 2001

• Created generic outline format for class.

Back to Tail Recursion, Continued. On to Multiple-Valued Procedures.

Disclaimer: I usually create these pages on the fly. This means that they are rarely proofread and may contain bad grammar and incorrect details. It also means that I may update them regularly (see the history for more details). Feel free to contact me with any suggestions for changes.

This page was generated by Siteweaver on Wed May 5 12:15:07 2004.
This page may be found at `http://www.cs.grinnell.edu/~rebelsky/Courses/CS151/2001S/outline.39.html`.