The Grinnell Scheme Web:
Procedure definitions

How does one define and name a new procedure in Scheme?

One way is to write a lambda-expression that has the new procedure as its value, and use it as the closing expression in a definition:

(define disparity
  (lambda (a b)
    (abs (- a b))))
There is an alternative notation, more concise and arguably more readable, for such definitions:
(define (disparity a b)
  (abs (- a b)))
The idea is to add the name that the programmer is bestowing on the procedure to the list of parameters from the lambda-expression to create a sort of template or specimen procedure call, in this case (cube num). The definition then consists of a left parenthesis, the keyword define, the template, the body of the procedure, and a right parenthesis.

Is there any difference in the meaning of the two forms?

None whatsoever. Use whichever one you prefer. Be prepared to encounter both in other programmers' Scheme code.

Which form would you recommend?

The short form, the second one. But it's a small stylistic point.


Next topic
Previous topic
Table of contents


This document is available on the World Wide Web as

http://www.math.grin.edu/~stone/scheme-web/procedure-definitions.html


created June 29, 1995
last revised December 30, 1995

Copyright 1995 by John David Stone (stone@math.grin.edu)