Building Abstractions with Procedures
1.1 The Elements of Programming
A powerful programming language is,
- instructing a computer to perform tasks
- serves as a framework within which we organize our ideas about processes
Thus, when we describe a language, we should pay particular attention to the means that the language provides for combining simple ideas to form more complex ideas.
- primitive expressions, which represent the simplest entities the language is concerned with
- means of combination, by which compound elements are built from simpler ones
- means of abstraction, by which compound elements can be named and manipulated as units
In programming, we deal with two kinds of elements: procedures and data. Thus, any powerful programming language should be able to describe primitive data and primitive procedures and should have methods for combining and abstracting procedures and data.
- data is “stuff” that we want to manipulate
- procedures are descriptions of the rules for manipulating the data
1.1.1 Expressions
One easy way to get started at programming is to examine some typical interactions with an interpreter for Scheme.
The leftmost element in the list is called the operator, and the other elements are called operands.
(+ 137 349)
The convention of placing the operator to the left of the operands is known as prefix notation. There are several advantages,
- It can accommodate procedures that may take an arbitrary number of arguments
- It extends in a straightforward way to allow combinations to be nested, that is, to have combinations whose elements are themselves combinations.
(+ 1 2 3 4 5 6 7 8 9 10)
;; combination over combinations
(+ (* 3 5) (- 10 6))
It reads an expression from the terminal, evaluates the expression, and prints the result. The interpreter runs in a “read-eval-print” loop. Lisp obeys the convention that every expression has a value.
1.1.2 Naming and the Environment
A critical aspect of a programming language is the means it provides for using names to refer to computational objects. We say that the name identifies a variable whose value is the object.
define
is scheme’s simplest means of abstraction, for it allows us to use simple names to refer to the results of compound operations. Complex programs are constructed by building, step by step, computational objects of increasing complexity.
(define size 2)
(* size 5) ;;10
It should be clear that the possibility of associating values with symbols and later retrieving them means that the interpreter must maintain some sort of memory that keeps track of the name-object pairs. This memory is called the environment (more precisely the global environment, since a computation may involve a number of different environments).