7. IPL vs.
FPL
Note that in imperative programming we concern ourselves
with both the computation sequence and maintaining the
program state (i.e. the collection of current data values).
Unlike IPLs, purely functional languages (no variables and
hence no assignments) have no equivalent concept of state:
the programmer focuses strictly on defining the desired
functionality.
Iteration is not accomplished by loop statements, but rather
by conditional recursion.
Functional programmers are concerned only with
functionality. This comes at a direct cost in terms of
efficiency, since the code is still translated into something
running on Von Neuman architecture.
A. Bellaachia Page: 13
8. Scheme overview
8.1. Get your own Scheme from MIT
swissnet.ai.mit.edu/projects/scheme/index.html
8.2. General overview
Scheme is a functional programming language
Scheme is a small derivative of LISP:
LISt Processing
Dynamic typing and dynamic scooping
Scheme introduced static scooping
Data Objects
An expression is either an atom or a list
An atom is a string of characters
A
Austria
68000
A. Bellaachia Page: 14
As in Lisp, a Scheme program is a set of
expressions written in prefix notation:
to add 2 and 3, the expression is (+ 2 3)
to subtract 2 from 3, the expression is (- 3 2)
to use the built-in function max to determine the maximum
value from 2, 3, and 17, the expression is (max 2 3 17)
8.3. Data Typing
Scheme uses dynamic typing (data types are
associated with values rather than with variables)
and uses static scoping for determining the visibility
of non-local variables.
8.4. Comments
Comments begin with a semi-colon
Example:
For instance, showing > as the prompt for
user input, a session might look like:
>; First some commentary, which won't get
evaluated
; below we will provide the postfix for
A. Bellaachia Page: 15
; 2+3, and then for (2+3)+6
; and finally for (2+3)-(2*2)
; we'll start the statements to be evaluated
; on the next line
(+ 2 3)
; Value: 5
>(+ (+ 2 3) 6)
; Value: 11
>(- (+ 2 3) (* 2 2))
; Value: 1
8.5. Recursion Instead of Iteration
Since we are expressing the entire computation as a
composition of functions into a single function,
recursion is usually used rather than iteration
Example:
>; the first line is the header for the Fibonacci
function:
(define Fibonacci (lambda (n)
; next is the termination case
( if (< n 3) 1
; and the recursive cal
(+ (Fibonacci (- n 1)) (Fibonacci (- n 2))))))
> (Fibonacci 6)
A. Bellaachia Page: 16