< 1. Printing and Drawing | 3. Strings and Variables > |
2. Simple Arithmetic
In this chapter, you will see how the four arithmetic operations are carried out in Scheme
Functions
In Scheme, a function is called (in other words, made to run) by writing its name inside parentheses:
(function-name argument-1 argument-2 argument-3)
The arguments of a function call are values that are passed to the function, which manipulates them according to the code contained in the function.
Adding numbers
The four arithmetic functions are defined as these symbols:
+ | addition |
- | subtraction |
* | multiplication |
/ | division |
Let's say we wish to calculate 15 + 75
. In Scheme, the function is the first element inside parentheses. The numbers to be added are written after the +
.
(+ 15 75)
Click on the code block above to get the answer: 90
.
What happens if +
is the only element?
(+)
We don't get an error. Instead, 0
is the return value. In the case of addition, 0
is its identity, meaning that adding anything to 0
returns the same value. Instead of generating an error, Scheme chooses to return 0
.
Multiplication also has an identity: 1
. Any number multiplied by 1
will stay the same.
(*)
However, subtraction and division by themselves will generate errors.
(-)
(/)
Scheme allows any number of arguments to be passed to the arithmetic functions. The effect is like repeatedly placing the operator in between the numbers in standard math notation:
(+ 1 2 3)
is the same as 1 + 2 + 3
(+ 1 2 3)
(- 50 10 2)
(* 2 10 5)
(/ 1000 2 10)
Let's revisit the parentheses issue. If we try writing +
and some numbers without the parentheses, the final number, 3
, is displayed, not the expected result of the calculation.
+ 1 2 3
Writing a calculation in standard notation will also "run", but again, the final number is returned, not the desired result of the calculation.
12 / 2
Writing the calculation in Scheme notation will give us the expected answer, 6
.
(/ 12 2)
Order of operations
Since Scheme makes it mandatory to write function calls inside parentheses, there is never any ambiguity about which calculation takes place first. As in standard math, the innermost calculations should be done first.
For example, is 2 + 3 * 4
equal to 2 + 12
or 5 * 4
?
;; with multiplication done first (+ 2 (* 3 4))
;; with addition done first (* (+ 2 3) 4)
It may take a bit to get used to writing operators and function names as the first element, but what may look like a major disadvantage right now is also Scheme's greatest strength—uniformity and the ability to represent code as lists of elements.
Writing your own function
Creating a function in Scheme looks like this:
(define (function-name parameter-1 parameter-2 more-parameters) function-body)
In most programming languages, you have to explicitly write return
followed by the value you wish to return (or no value at all).
However, in Scheme, the last value inside the (define ...)
definition is implicitly returned.
Convert miles to inches
Let's write a simple function to convert miles to inches. One mile is 5280
feet, and one foot is 12
inches. So, we have:
(define (miles-to-inches miles) (* miles 5280 12))
One mile is:
(miles-to-inches 1)
And three miles are:
(miles-to-inches 3)
< 1. Printing and Drawing | 3. Strings and Variables > |