The Grinnell Scheme Web: The - procedure

How about subtraction? How does that work in Scheme?

Call the - procedure, giving it the minuend as the first operand and the subtrahend as the second:

> (- 128 71)
57
> (- 129837981749809183094102634 23098172098709187098273)
129814883577710473907004361
> (- 31 47)
-16
> (- -12 -23)
11
Must all the numbers be integers?

No. Like the + procedure, - takes accepts as operands any numbers that are supported by the implementation.

What will the - procedure do if you give it a different number of operands?

If you give it just one operand, it returns the negative (the additive inverse) of the number:

> (- 12)
-12
> (- -43)
43
> (- 0)
0
Giving it no operands at all is an error that will probably bring your program to an abrupt halt:
> (-)

ERROR: -: Wrong number of args
(In this advisory message, produced by the SCM implementation of Scheme, the word ``args'' is short for ``arguments.'' An argument is an expression that is evaluated to obtain an operand for a procedure.)

The Scheme standard does not require an implementation of the - procedure to make any provision for more than two operands. Some Scheme implementations (SCM and Elk, for instance) treat any extra operands as additional subtrahends:

> (- 12 1 2 3 4)
2
Others (Scheme 48, for instance) consider such calls erroneous:
> (- 12 1 2 3 4)

Error: wrong number of arguments
       (- 12 1 2 3 4)
The careful programmer will avoid giving - more than two operands.


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/minus.html


created June 23, 1995
last revised December 29, 1995

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