Skip to main content

Elements of Programming in Scheme

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.
  • primitive expressions, which represent the simplest entities the language is concerned with,
  • means of combination, by which compound elements are built from simpler ones, and
  • means of abstraction, by which compound elements can be named and manipulated as units.
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.
Compound procedures
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

Popular posts from this blog

Backbone.js - An Introduction

Backbone.js is a lightweight JavaScript library that adds structure to your client-side code.  Developers commonly use libraries like Backbone.js to create single-page applications (SPAs).  What is a Single-Page-Application? A single page Web App is a website that attempts to recreate the experience of a native desktop or mobile application. On a Single Page Web App you are less likely to see page refreshes, the browser navigation within the app is often overridden and  there will usually be some offline capabilities. To a user their interaction with a Single Page Web App will be almost identical to how they interact with a full native application. Backbone has many advantages over other js frameworks. Backbone is mature, popular, and has a number  of plugins and extensions available that build upon it. It has been used to create non-trivial applications by companies such as Disqus, Walmart, SoundCloud and LinkedIn. Backbone organizes its whole code under t...

Abstraction Using Higher Order Procedures

Procedures are, in effect, abstractions that describe compound operations on numbers independent of the particular numbers. Yet even in numerical processing we will be severely limited in our ability to create abstractions if we are restricted to procedures whose parameters must be numbers. Often the same programming pattern will be used with a number of different procedures. To express such patterns as concepts, we will need to construct procedures that can accept procedures as arguments or return procedures as values. Procedures as arguments Procedures are often used as the arguments of an another procedure. This provides a higher level of abstraction in programming. For example consider a procedure to find out the sum of squares of numbers between a range. The procedures inc and square are used as the arguments of the procedure sum, which is generalised as follows.                 (define (inc...