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
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 .
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> |
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
Post a Comment