What about division, then? How do I divide one number by another in Scheme?
Call the / procedure, giving it the dividend as the first
operand and the divisor as the second:
If the operands are both integers and the division comes out even, Scheme returns an integer. If it doesn't come out even, the kind of number returned varies from one implementation to another. For example, SCM (which does not support rationals) returns a real approximation to the quotient, as shown above, while Scheme 48 returns a rational:> (/ 76 19) 4 > (/ -300 25) -12 > (/ 82 34) 2.4117647058824
The rare implementation that supports only integers is not required to provide the> (/ 82 34) 41/17
/ procedure at all.
Does / accept operands that are not integers?
Yes. It accepts numbers of any kind that is supported by the implementation:
What will the> (/ 3/4 1/12) 9 > (/ 7.2+6.1i -4.3i) -1.4186046511628+1.6744186046512i
/ procedure do if you give it a different number
of operands?
If you give it just one operand, it returns the reciprocal (the multiplicative inverse) of the operand:
Giving it no operands at all is an error, just as in the case of the> (/ 5) 1/5 > (/ 21/5) 5/21 > (/ -2i) 0+1/2i
- 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 divisors:
Others consider such calls erroneous:> (/ 2520 3 5 7) 24
> (/ 2520 3 5 7)
Error: wrong number of arguments
(/ 2520 3 5 7)
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/slash.html