The eval function is the function that Scheme itself
uses to evaluate expressions.
(define sum
(lambda n
(cond ((null? n) 0)
( else (eval (cons '+ n)))
)))
This solution introduces two new elements of Scheme syntax. To understand this version, you need to know
that the single quote before the ???+??™ stops Scheme from evaluating the function ???+??™, and instead forces Scheme
to treat ???+??™ as a simple character atom. In addition, the function cons creates a new list by adding an element
to the front of a list, in this case adding the ???+??™ to the front of the list of numbers to be summed.
Functional programming has the desirable properties of simple syntax and semantics, and compact code.
Also, since a function may not change any of the parameters passed to it, and since assignment is not used
to change program state, ???side effects??? (any changes in variables that endure after execution of the code) are
eliminated, with resulting improvements in reliability.
Historically, functional programming has found advocates in the fields of artificial intelligence and expert
systems. The popular editor Emacs is written in LISP, too, as is the on-line fare search program employed by
Orbitz (http://www.
Pages:
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169