For example,
to add five numbers, we simply increase the number of arguments:
(+ 3 5 7 4 2 )
21
Expressions can be evaluated as arguments, too, so this Scheme expression divides the sum of 3 and 5 by
the difference between 7 and 5:
( / (+ 3 5 ) ( - 7 5) )
4
Another primitive function in LISP and Scheme is the function list, which takes a series of arguments
and makes a list:
( list 1 5 6 )
( 1 5 6 )
If we need the first element in a list, the function car will return that:
( car (list 1 5 6) )
1
The function cdr will return all but the first element of the list:
( cdr (list 1 5 6) )
( 5 6 )
56 SOFTWARE [CHAP. 4
A list can include many elements, only one element, or no elements:
( list 8 )
( 8 )
( list )
()
One can define a new function at any time using the ???lambda notation.??? This code creates a new
function called ???sum??™ which adds two numbers together, just like the built-in primitive ???+??™ does, but for only two
arguments:
(define sum
(lambda (n m)
( + n m)))
The name sum is associated with a function that expects two arguments. The function completes after
adding the arguments n and m together, and the result of evaluating the function is the sum of the two arguments.
Pages:
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163