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:
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(define (disparity a b) (abs (- a b)))
(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