A powerful programming language provides a framework to interpret our ideas of processes. It generally combines small procedures to complex ones. Usually there are three steps to accomplish this.
Some very basic ideas in manipulating with the data are given below.Expressions
A very simple expression is a number. Simple expressions are combined to form complex ones. For example:
(+ 2 3 4)
is an expression where + is the operation and 2,3,4 are the oparents. The expression can be made more complex by combining simple expressions as given below:
(+ (* 3 (+ (* 2 4)
(+ 3 5)))
(+ (- 10 7)
6))
Naming and the Environment
In scheme we use define for naming. For example, (define length 10) assigns the value 10 to the variable length and this value can be used for further operations.
define is the simplest means of abstraction in scheme. Since complex programmes are developed by step by step manner, define acts as a basic step of abstraction.
Eg:
(define a 10)
(define b 20)
(define c (+ a b) )
Evaluating Combinations
To evaluate a combination, do the following:
1. Evaluate the subexpressions of the combination.
2. Apply the procedure that is the value of the leftmost subexpression (the operator) to
the arguments that are the values of the other sub expressions (the operands).
For example, consider the following expression
(+ (* 4 (+ 2 3))
(+ 2 (* 9 3))
3)
Here there are five levels of evaluation. Each expression is split into many sub expressions and evaluated separately. Thus this is a recursive process. We take care of each primitive case by considering the following points
- the values of numerals are the numbers that they name,
- the values of built-in operators are the machine instruction sequences that carry out the corresponding operations, and
- the values of other names are the objects associated with those names in the environment.
Wee now look upon how to define procedures and produce a higher level of abstraction. For example consider a procedure to calculate the square of a number. It is defined as given below.
(define (square x) (*x x))
That is (square 2)will return the value 4. The formal structure of a procedure defenition is as follows.
(define (<name> <formal parameters>) <body>)
The <name> is a symbol to be associated with the procedure definition in the environment. The <formal parameters> are the names used within the body of the procedure to refer to the corresponding arguments of the procedure. The <body> is an expression that will yield the value of the procedure application when the formal parameters are replaced by the actual arguments to which the procedure is applied.
Conditional Expressions and Predicates
By the above described simple procedure definition we have no way to make tests and to perform different operations depending on the result of a test. So for solving this problem the conditional expressions are introduced. The general form of a conditional expression is
(cond (<p1> <e1>)
(<p2> <e2>)

(<pn> <en>))
Conditional expressions are evaluated as follows. The predicate <p1> is evaluated first. If its value is false, then <p2> is evaluated. If <p2>'s value is also false, then <p3> is evaluated. This process continues until a predicate is found whose value is true, in which case the interpreter returns the value of the corresponding consequent expression <e> of the clause as the value of the conditional expression. If none of the <p>'s is found to be true, the value of the cond is undefined. For example, consider the procedure to find the absolute value. This can be written using conditional statements as follows
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
Another conditional statement is the if statement. The general form of a if statement is as follows:
(if <predicate> <consequent> <alternative>)
To evaluate an if expression, the interpreter starts by evaluating the <predicate> part of the expression. If the <predicate> evaluates to a true value, the interpreter then evaluates the <consequent> and returns its value. Otherwise it evaluates the <alternative> and returns its value.
The above example to calculate the absolute value can be written using if statement as follows:
(define (abs x)
(if (< x 0)
(- x)
x))
Compound predicates are formed using some logical composition operations. The three important such operations are given below:
- (and <e1> ... <en>)
The interpreter evaluates the expressions <e> one at a time, in left-to-right order. If any <e> evaluates to false, the value of the and expression is false, and the rest of the <e>'s are not evaluated. If all <e>'s evaluate to true values, the value of the and expression is the value of the last one.
- (or <e1> ... <en>)
The interpreter evaluates the expressions <e> one at a time, in left-to-right order. If any <e> evaluates to a true value, that value is returned as the value of the or expression, and the rest of the <e>'s are not evaluated. If all <e>'s evaluate to false, the value of the or expression is false.
- (not <e>)
The value of a not expression is true when the expression <e> evaluates to false, and false otherwise.
Please refer here for more details regarding the elements of programming in scheme.
Comments
Post a Comment