; Value: 8
8.6. Evaluation
The functional approach sometimes requires us to
take a "bottom-up" view of the problem: creating
functions to compute the lowest layer of values,
then other functions taking those as operands.
Example: Design a code to compute (a + b + c) / (x
+ y + z)
Compute the numerator and denominator
separately,
; for the numerator
(+ a b c)
; for the denominator
(+ x y z)
and then decide how to apply division with those
two functions as operands, i.e.:
(/ (+ a b c) (+ x y z))
8.7. Storing and using Scheme code
The load function is available to load a Scheme
program stores in a an text file, e.g.:
> (load "myfile.txt")
; Loading "myfile.txt" -- done
A. Bellaachia Page: 17
8.8. Variables
Variables are always bound to values
To declare and initialize a variable, we use the built in
define command, giving it the variable name and the value
it is to be initialized with (the value may be an expression)
Examples:
> (define x 3)
; Value:x
> (define foo (+ 4 7))
; Value: foo
Check the content of a variable:
>x
; Value: 3
>foo
; Value: 11
A. Bellaachia Page: 18
8.9. Data types
Literals are described as self-evaluating, in that
evaluating the literal returns the value they
represent. (E.g. evaluating 3 returns the
integer value 3.)
The primitive types are:
characters
strings (in double-quotes)
Booleans:
True: #t
False: The empty set for false or
#f (see example below).
Integers
rational numbers
real numbers
complex numbers.
List: There is also a composite data type,
called the list, which is a fundamental part
of Scheme. Lists are considered in detail in a
later section.
Numbers
There are integers, rationals, reals, and complex
numbers.
In general, Scheme will return as exact an answer as it
can (i.e. it will give an exact integer or rational over a
real approximation).
Examples:
Let's see the results of some basic arithmetic:
>(/ 3.2 1.6)
A. Bellaachia Page: 19
; Value: 2.
>(/ 16 10)
; Value: 8/5
Suppose we were to try some comparisons:
>(< 2 3)
; Value: #t
>(< 4 3)
; Value: ()
8.10. Arithmetic functions
There are many built-in arithmetic functions. Some of
the commonly used ones include:
max, min
+, *, -, /
quotient, modulo, remainder
ceiling, floor, abs, magnitude, round, truncate
gcd, lcm
exp, log, sqrt
sin, cos, tan
There are also a number of comparison
operators returning Boolean values
A. Bellaachia Page: 20