I'm trying to learn about declarative programming, and while reading the wiki it said the following:

In computer science, declarative programming is a programming paradigm—a style of building the structure and elements of computer programs—that expresses the logic of a computation without describing its control flow.

What exactly does this mean? I am struggling to find a definition that effectively differentiates the two terms, especially in the context of declarative programming. Is it just describing a type of abstraction?

Thanks for your help.

P.S Would it help if i could tag this as declarative programming? I can't create the tag myself

  • $\begingroup$ I added "logic programming", which I think is a reasonable near-synonym. $\endgroup$ – David Richerby Jan 12 '17 at 20:18

Control flow is oriented around the idea of imperative statements. What is the computer doing at this given time? What will it do next? What actions should it perform?

Declarative programming abstracts this away (or at least tries to). You're program is now not a set of instructions for the computer to follow, but a sort of specification for the problem. In order to be powerful enough to be Turing complete, these specifications are usually recursive.

The main difference is that a declarative program makes no promises about how it will be run. You state the logic, and the compiler/interpreter makes a program that fulfills that logic.

One of the most important aspects of a declarative description is that it's (relatively) standalone. A procedure in an imperative language says "do this thing to the current state". A declarative program says often will not depend on its context, or will make its context more explicit.

In practice, you can usually infer the control flow of a declarative program pretty easily, especially in strict languages like ML, F# or Elm. There's a pretty close correspondence between recursive declarations and loops, and between declarative functions and imperative procedures.

Things get fuzzier in a language like Haskell, where evaluation is lazy (you don't decide what gets run when). Or in Prolog, where there's an implicit backtracking search that the runtime does for you. You're not coding these things manually, you're simply specifying what the result should look like, not how you will get that result.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.