tags:

views:

2587

answers:

11
  1. In a nutshell, what is:

    • Functional programming
    • Declarative programming
    • Imperative programming
  2. Are there other (more exotic) types?

  3. What type is jQuery? I really like it but don't know what it's called.

+3  A: 

Wikipedia is a good place to start with this type of question

jQuery is a javascript library, like scriptaculous.

Don Branson
This isn't very far from a 'GIYF' answer.
Geoffrey Chetwood
yes. my intent was to politely communicate to him that he needs to put at least a little effort into finding the answer - 10 or 20 seconds, before posting to SO. I've seen others out here rudely give the RTM answer, but i think we can be nice about it instead.
Don Branson
@Don: He /doesn't/ need to google it first. That is the point of SO.
Geoffrey Chetwood
@Rich. Huh. Okay. I've heard just the opposite opinion, too. Guess I'll have to go read the FAQ and find out for myself.
Don Branson
@Don: Believing everything you hear is not usually a good thing. When in doubt, read the FAQ.
Geoffrey Chetwood
@Rich I have read the FAQ, so now I know the authoritative answer. Shame on me for not reading it before. I hang my head. :(
Don Branson
+10  A: 

1) There's not really any non-ambiguous, objective definition for these. Here is how I would define them:

imperative - The focus is on what steps the computer should take rather than what the computer will do (ex. C, C++, Java).

declarative - The focus is on what the computer should do rather than how it should do it (ex. SQL).

functional - a subset of declarative languages that has heavy focus on recursion

2) There are some multiparadigm languages that kind of straddle both sides. For example, C#, Python, and JavaScript are mainly imperative languages that have some functional or declarative elements. There are also logic programming languages (like prolog) that mainly focus on satisfying constraints.

3) I believe that JQuery falls under the multiparadigm category above (like the language it's implemented in, JavaScript).

Jason Baker
+1 - very nicely summarized.
duffymo
Recursion isn't the only feature of functional languages. They also have ways of dealing with functions other than just compilation, perhaps for example allowing runtime combination of functions to get a new function.
David Thornley
Keep a couple of things in mind: 1) the explanation is intended to be simple rather than all-inclusive 2) like I said, there are multiple ways to define these languages. Thus, the answer could very well be wrong to you and right to someone else.
Jason Baker
+9  A: 

From Wikipedia

In computer science, declarative programming is a programming paradigm that expresses the logic of a computation without describing its control flow. It attempts to minimize or eliminate side effects by describing what the program should accomplish, rather than describing how to go about accomplishing it. This is in contrast from imperative programming, which requires a detailed description of the algorithm to be run.

Declarative programming consider programs as theories of a formal logic, and computations as deductions in that logic space. Declarative programming has become of particular interest recently, as it may greatly simplify writing parallel programs.

Common declarative languages include those of regular expressions, logic programming and functional programming.

From Wikipedia:

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state. Functional programming has its roots in the lambda calculus, a formal system developed in the 1930s to investigate function definition, function application, and recursion. Many functional programming languages can be viewed as embellishments to the lambda calculus.

From Wikipedia:

In computer science, imperative programming is a programming paradigm that describes computation in terms of statements that change a program state. In much the same way as the imperative mood in natural languages expresses commands to take action, imperative programs define sequences of commands for the computer to perform.

The term is used in opposition to declarative programming, which expresses what needs to be done, without prescribing how to do it in terms of sequences of actions to be taken. Functional and logical programming are examples of a more declarative approach.

jQuery is not a programming language, it is a library for the JavaScript programming language.

TheTXI
Tried to blockquote the last two parts from wikipedia, but was getting an unresponsive script error in firefox.
TheTXI
Those wikipedia articles are almost not understandable if you don't know the terms beforehand. (At least for me.)
Georg
duplicate answer
Don Branson
+1: not sure why this answer was downvoted when an identical answer got 2 upvotes.
Juliet
because it's an identical answer. i think the common practice here is to check previous posts before posting, and it's pretty common to see subsequent answers downvoted. Also, it's probably better to tell the guy where to fish, instead of fishing for him.
Don Branson
This site tends to provide the information in the answer more than just telling the user to google it or to point them elsewhere. When I first checked your post, it did not have the URL's as clickable.
TheTXI
sure, but people here have the expectation that you've at least put a little effort into looking it up.
Don Branson
@Don: Nope. This site does not do the typical GIYF expectation. This is clearly stated in the FAQ. This answer is much better than you just posting a bunch of potentially related links. Remember, SO is meant to be a wiki over time.
Geoffrey Chetwood
@Rich B. Okay. I've heard the opposite viewpoint from other commenters. I don't think I disagree with what you're saying, but there deosn't really seem to be a consensus among SOers as to the best approach.
Don Branson
@Don: There will always be dissenting ranks, but the FAQ is clear on this and other issues.
Geoffrey Chetwood
@Rich: Sounds good. thanks.
Don Branson
@belgariontheking: Your reasoning is cogent and compelling. You have persuaded me to align with your point of view.
Don Branson
@Don Branson, I agree with RichB. Regardless of how simple or obvious the answer is, we welcome all questions. We are looking to become a repository of knowledge in semi-wiki form.
Simucal
+1  A: 

This video from channel9 with Erik Meijer helped me a lot in understanding what functional programming really is.

A good point in the video starts around 16 minutes.

Ólafur Waage
+1  A: 

I'd add Object-Oriented programming to the list of styles (Wikipedia). Maybe Esoteric languages (Wikipedia) as well, as they focus on how the code looks and not how the program actually runs.

Damoon Rashidi
+1  A: 
  1. I think that your taxonomy is incorrect. There are two opposite types imperative and declarative. Functional is just a subtype of declarative. BTW, wikipedia states the same fact.
  2. Probably multiparadigm languages that incorporates features from both worlds. So I share opinion of Jason Baker.
  3. Again I share opinion of Jason Baker. jQuery is multiparadigm as its host language is.
Rorick
+1: Yep, the paradigms are apples and oranges.
Nikhil Chelliah
+12  A: 

Functional programming is a subtype of declarative programming. So you've really asked the question "what is functional/declarative programming versus imperative programming"?

Imperative Programming is what most professional programmers use in their day-to-day jobs. It's the name given to languages like C, C++, Java, COBOL, etc. In imperative programming, you tell the computer what to do. "Computer, add x and y," or "Computer, slap a dialog box onto the screen." And (usually) the computer goes and does it. This is where most of us spend our lives, in looping structures and if-then-else statements and the like.

Functional Programming, as far as I understand it, seeks to describe what you want done rather than specify how you want something done. It's probably best understood in contrast to imperative programming. For instance, if you have a list in C and you want to pull out every Nth element, you have to point at the first element, set a counter at one, move to the next element, increment the counter, check to see if you're at the Nth element and so on. The functional equivalent would be to write a function that recognizes when the size of a list is a multiple of N, and then pass that function to the list, possibly with another snippet of code to hand back the head of the list if your N-recognizer evaluates to true and discarding it if it evaluates to false. The two functions recurse through the list, and finally hand back a list consisting of every Nth element.

The latter method might seem like the more confusing way to go about things, and that's because it is. Functional programming can be a mind-bender, which is one reason why Lisp, Scheme, and Haskell have never really surpassed C, C++, Java and COBOL in commercial popularity. But there are benefits to the functional way. For one, if you can get the logic correct, functional programming requires orders of magnitude less code than imperative programming. That means fewer points of failure, less code to test, and a more productive (and, many would say, happier) programming life. As systems get bigger, this has become more and more important.

Are there more exotic types? Not yet. There are hybrids between the two of them (like Scala), but these merely seek to leverage the strengths of both types. Then there's Object-oriented programming, which is really just a new way to organize data in an imperative program. And even with strange new technologies like quantum computing, the (planned-for) underlying languages fall somewhere in the declarative/imperative spectrum.

And, as others have pointed out, JQuery is a library that sits on top of JavaScript, which is itself a hybrid functional/imperative language. Without going into too much detail, JavaScript is like the ugly, buck-toothed girl your parents forced you to take to the prom. JQuery is like the fairy godmother who swoops in and, with one sweep of the wand, turns her into a total babe before your eyes. (See Douglas Crockford's JavaScript: the Good Parts to learn more.)

rtperson
I agree with your answer, but I don't think that you can say that Javascript is bad. What's wrong with it are the implementations of the browsers, the language itself is good and easy to use, yet powerful at the same time.
Georg
I'm not saying JavaScript is bad, just ugly. :) It was pushed out into the world before it was ready, and it can be a pain in the neck to work with because of this. The heart of the language, though, is beautiful, simple, and powerful.
rtperson
I like the explaination and loved the JavaScript/JQuery analogy!
AieshaDot
+6  A: 

In a nutshell:

An imperative language specfies a series of instructions that the computer executes in sequence (do this, then do that).

A declarative language declares a set of rules about what outputs should result from which inputs (eg. if you have A, then the result is B). An engine will apply these rules to inputs, and give an output.

A functional language declares a set of mathematical/logical functions which define how input is translated to output. eg. f(y) = y * y. it is a type of declarative language.

jQuery is a library.

It is built in an imperative language (JavaScript, which has absorbed some of the paradigms of functional programming) which makes use of declarative selector rules (based on CSS) to interact with browser objects (the DOM).

mdja
A: 

I am being assigned to research F#. AFAIK, it definitely is a multi-paradigm language. It has the the best of both worlds. It could be used by new programmer (easy to learn, easy to understand), commercial programmer (productivity, stable, excellent supported .NET library), scientist programmer (good performance on manipulate large data, expressive mathematical syntax).

A: 

In a nutshell, the more a programming style emphasizes What (to do) abstracting away the details of How (to do it) the more that style is considered to be declarative. The opposite is true for imperative. Functional programming is associated with the declarative style.

jchadhowell
A: 

Declarative programming is programming by expressing some timeless logic between the input and the output, for instance, in pseudocode, the following example would be declarative:

def factorial(n):
  if n < 2:
    return 1
  else:
    return factorial(n-1)

output = factorial(argvec[0])

We just define a relationship called the 'factorial' here, and defined the relationship between the output and the input as the that relationship. As should be evident here, about any structured language allows declarative programming to some extend. A central idea of declarative programming is immutable data, if you assign to a variable, you only do so once, and then never again. Other, stricter definitions entail that there may be no side-effects at all, these languages are some times called 'purely declarative'.

The same result in an imperative style would be:

a = 1
b = argvec[0]
while(b < 2):
  a * b--

output = a

In this example, we expressed no timeless static logical relationship between the input and the output, we changed memory addresses manually until one of them held the desired result. It should be evident that all languages allow declarative semantics to some extend, but not all allow imperative, some 'purely' declarative languages permit side effects and mutation altogether.

Declarative languages are often said to specify 'what must be done', as opposed to 'how to do it', I think that is a misnomer, declarative programs still specify how one must get from input to output, but in another way, the relationship you specify must be effectively computable (important term, look it up if you don't know it). Another approach is nondeterministic programming, that really just specifies what conditions a result much meet, before your implementation just goes to exhaust all paths on trial and error until it succeeds.

Purely declarative languages include Haskell and Pure Prolog. A sliding scale from one and to the other would be: Pure Prolog, Haskell, OCaml, Scheme/Lisp, Python, Javascript, C--, Perl, PHP, C++, Pascall, C, Fortran, Assembly

Lajla