Skip to main content

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 n) (+ n 1)
                (define (square x) (* x x))    
                (define (sum term a next b)
                  (if (> a b)
                      0
                      (+ (term a)
                         (sum term (next a) next b))))

                (define (sum-square a b)
                  (sum square a inc b))


Constructing procedures using lambda
 lambda is a special form which creates procedures. In general, lambda is used to create procedures in the same way as define, except that no name is specified for the procedure:


                (lambda (<formal-parameters>) <body>)

The resulting procedure is just as much a procedure as one that is created using define. The only difference is that it has not been associated with any name in the environment. For example we can define a lambda expression to add 4 to a number as follows.

                                        ( lambda x (+ x 4)) 

Like any expression that has a procedure as its value, a lambda expression can be used as the operator in a combination also.
 
Using let to create local variables 
The general form of a let expression is


                (let ((<var1> <exp1>)
                      (<var2> <exp2>)
                      
                      (<varn> <expn>))
                  <body>)


which can be thought of as saying             

  let <var1> have the value <exp1> and

<var2> have the value <exp2> and


<varn> have the value <expn>
in <body>
The first part of the let expression is a list of name-expression pairs. When the let is evaluated, each name is associated with the value of the corresponding expression. The body of the let is evaluated with these names bound as local variables.For example consider the following function:
                                      f(x,y) = x^2+(1+y)x+y(1-x)
This function can be defined using let expression as follows:

               (define (f x y)
                 (let ((a (* x (+ 1 y)))

                       (b (* y (- 1 x))))
                   (+ (square x) a b)))


We can also use define in place of let. But let is preffered generally. 

Procedures as returned values
 We can enhance the expressive power of our programming language by creating procedures whose returned values are themselves procedures. For example consider the following statements.
 
               (define (sample-proc f)
                 (lambda (x) (proc x (f x))))

 
sample-proc is a procedure that takes as its argument a procedure f and returns as its value a procedure (produced by the lambda) that, when applied to a number x, produces the value after applying the proc function to arguments x and (f x).

More details regarding providing abstraction with higher order procedures is provided here .

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...

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:              ...