Imperative and Declarative Languages

The languages discussed up to this point have all been 'imperative' languages. Such languages are all written in the imperative voice, taking the form of commands or instructions to the machine. Declarative languages offer an entirely different perspective on writing programs. Declarative languages take the form of declaring the nature of functions, operations, and relationships in code. From this perspective, it is the job of the compiler or interpreter to determine how best to implement these definitions in machine code. The most important family of declarative languages are the functional languages. Code written in these languages focuses primarily on defining and referencing functions.

The distinction between imperative and declarative languages is somewhat analogous to the distinction between a recipe and a blueprint. A recipe gives a set of steps in order to accomplish a goal, but does not describe what that goal state should look like. A blueprint gives a precise description of what the goal should look like, but does not specify how to accomplish it. The paradigm behind functional languages is that the compiler or interpreter should be able to figure out, using a set of well-understood principles, how to accomplish a goal if it is sufficiently well described. Imperative programming assumes that a computer is an automaton which must be given explicit instructions for every step toward a goal.

An example program of the same population-growth model now written in the functional language, Standard ML, is as follows:

val N = 20; val rl = 0.5; val il = 2.0; val r2 = 0.02 5; val i2 = 42.0;

(* this function will print the contents of our list *) fun printList(nil) = () printList(h::t) =

print( Real.toString(h) ); print("\n"); printList(t)

(* this function will compute the values of our population *) fun popn( 0, pO, r, L ) = p0::L

popn( t : int, pO : real, r : real, L ) = let val p = pO * Real.Math.exp( r * real(t) )

print("Computing Int.toString(N) steps of the pop. growth") print (11 with initial pop. Real. toString (il) ); print(" and growth factor Real.toString(rl) A"\n"); printList( popn(N-1,il,rl, [] ) );

print ("Computing Int. toString (N) *11 steps of the pop. growth") print (11 with initial pop. "* Real. toString (i2 ) ); print (11 and growth factor Real. toString (r2 ) printList( popn(N-l,i2,r2, [] ) );

Functional languages make a great deal of use out of 'recursion'. Recursion occurs when a function calls itself with slightly different arguments. A classic example of recursion is the Fibonacci sequence (0, 1, 1, 2, 3, 5, 8, 13,...) where each value (except for the first two) is defined as the sum of the previous two values. Recursion can be thought of as similar to mathematical induction, though there must by necessity be some termination to the recursion. In the above code, both the popn and printList functions are defined in terms of themselves. This reflects the nature of the population growth model, in which the size of a subsequent population is dependent upon the size of the current population.

It can be proved that the loops which are so common in imperative programs are functionally equivalent to the recursion which characterizes functional programming. However, there are specific algorithms which cannot be implemented as efficiently using one technique as they can with another. This, and the fact that imperative languages are much more popular than functional languages, has led most functional languages to include some imperative features. More recently, some imperative languages have also begun to adopt some functional style syntax as well. One of the advantages to software development of following 'good functional style' in programming is that it can dramatically reduce the number of bugs by eliminating so-called 'side effects'. Side effects are changes made to the internal state of the computer system whose ramifications are not entirely visible within the code.

Historically declarative languages were used almost exclusively as theoretical tools within the academic world. Outside of computer science research circles, the perception generally was that functional languages were unnecessarily complex and computationally inefficient. Recently, however, a number of new languages have emerged which may begin to bring functional languages to the more mainstream software development community. MLton is an optimizing compiler for standard ML which uses numerous intermediate languages to generate very well-optimized binary code. Objective Caml (Ocaml) is both a functional and object-oriented language. F# (pronounced 'eff-sharp') is a functional language developed by Microsoft Research which combines functional programming with the facilities of the .Net runtime environment. The Python language also incorporates some functional properties.

Other Language Types

Other types of computer languages which are significant but not usually considered programming languages include query languages, communication protocols, and markup languages. Query languages are often used to interact with sophisticated data services. Query languages formalize the ways in which users or software applications can access and manipulate data stored in central repositories. This allows many users and many different software applications to access the data in a way which is independent of the use of that data, or how that data is organized within the repository. The most common query language is Structured Query Language (SQL) which is used to interact with relational databases.

A communication protocol is a form of computer language which formalizes the transmission of information. It helps to ensure correct and accurate receipt of the information and to coordinate communication between many computer systems. Some communication protocols are intended as transport layers, that is, they serve to transport other forms of communication. The most common example is the TCP/IP suite of protocols, which form the basis of the Internet. All Internet communications, including the World Wide Web (WWW), e-mail, and instant messenger communications are transported between the sender and recipient computers via the TCP/IP protocols. The HTTP protocol which forms the basis for WWW communications is itself a separate protocol from TCP/IP, but is dependent on TCP/IP for transmission. Protocols like HTTP are very similar to query languages in that they are used to request data in various formats between users (known as clients) and central repositories (known as servers). The profusion of different internet services has led also to a wide variety of new communications protocols, each one tailored for a particular task.

Markup languages are languages used to format data in specific ways. Typically, a markup language involves embedding the 'content' of a document within various markup elements, commonly called 'tags', which indicate how the content should be displayed. Markup tags can also indicate the functional role that various elements of the document's content plays in the interpretation of that content. HTML, the language that websites are written in, is the most familiar example of a markup language. XML is similar in many ways to HTML, but is not specific to web page formatting. It is a popular new markup language, and will likely soon eclipse HTML in terms of importance.

It is debateable whether there are formal distinctions between programming languages and other forms of computer languages. Generally, programming languages are seen as the procedural parts of a computer, they describe the ways in which the computer will behave. Query languages, communications protocols, and markup languages, on the other hand, are just ways of encoding content of computer programs. This distinction between data and processing, though central to many paradigms of computation, may be artificial. A computer program which receives an SQL query or an HTML document will certainly adjust its behavior based upon the content encoded therein, much the way an interpreter will adjust the behavior of the computer to respond to different lines of code in a scripting language. Most contemporary computer security issues have to do with unforeseen ways in which such software can be manipulated by malicious alteration of the contents of a document or communications protocol. It is true, however, that these languages are very purpose specific, whereas other 'true' programming languages are able to be very general. Within general speech, however, it would be perfectly comprehensible for someone to say that they 'program' in HTML or SQL, despite these not being generally accepted as programming languages by the programming community.

10 Ways To Fight Off Cancer

10 Ways To Fight Off Cancer

Learning About 10 Ways Fight Off Cancer Can Have Amazing Benefits For Your Life The Best Tips On How To Keep This Killer At Bay Discovering that you or a loved one has cancer can be utterly terrifying. All the same, once you comprehend the causes of cancer and learn how to reverse those causes, you or your loved one may have more than a fighting chance of beating out cancer.

Get My Free Ebook

Post a comment