Thursday, May 24, 2007

High-order Procedures

Most modern programming languages provide certain abilities to blur the traditional distinction between ``passive'' data and ``active'' process, one of the most well-known techniques might be so-called OOD, i.e., Object Oriented Design. Languages such as Lisp in traditional way provide programmers the power to handle precedures as data, which pioneered a new programming flavor called Functional Programming, or, FP in short (Now some FP languages also implemented OO facilities). Procedures that manipulate procedures are called higher-order procedures.

Put aside the consideration of efficiency, we can implement some atomic operations such as cons, car and cdr like following:

(define (cons x y)
  (lambda (f) (f x y)))

(define (car z)
  (z (lambda (p q) p)))

(define (cdr z)
  (z (lambda (p q) q)))

Here, cons just returns a procedure which takes x and y as parameters. car and cdr applies the procedure constructed above with parameter (a procedure) which returns the former and latter parameter, respectively.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home