Declarative programming

Declarative programming is a programming paradigm in which the description of the problem is in the foreground. The approach is then determined automatically. In contrast to imperative programming, in which the As is in the foreground, one wonders in the declarative programming after Is this supposed to be calculated. Known representatives of declarative programming languages ​​are Haskell, LISP, Prolog, XAML, and in a broader sense, SQL and XSLT. The declarative languages ​​are compared to the more common imperative languages ​​such as C, C or Java.

The differences between the two approaches are most evident in the implementation of an algorithm that can be regarded as a combination of labor and control mechanism:

  • Declarative languages ​​enable separation of the two components.
  • In contrast, when using an imperative programming language, a separation of work and control mechanism is hardly possible. Imperative languages ​​describe computation processes; thus can be understood as imperative programs instructions to the machine on which they run.

Declarative languages

The declarative programming languages ​​include:

  • Query languages ​​(eg SQL)
  • Functional- logic languages ​​(eg Babel, Escher, curry, Oz)
  • Functional languages ​​(eg, LISP, ML, Miranda, Gofer, Haskell, Erlang )
  • Logical languages ​​(eg Prolog )
  • Synchronous programming languages ​​( eg, Lustre )
  • Build tools ( eg make or Ant)
  • Transformation languages ​​(eg XSLT)

Example

The Quicksort sorting algorithm can be written in the imperative programming language Pascal as follows:

Procedure quicksort ( l, r: integer);   var x, i, j, tmp: integer;   begin     if r > l then     begin       x: = a [ l]; i: = l; j: = r 1;       repeat         repeat i: = i 1 until a [i ]> = x;         repeat j: = j-1 until a [j ] < = x;         tmp: = a [ j]; a [j ]: = a [i ]; a [i ]: = tmp;       until j < = i;       tmp: = a [ j]; a [j ]: = a [ l]; a [ l]: = tmp;       Quicksort (l, j -1);       quicksort ( j 1, r)     end   end; The programmer describes how the algorithm must run. It is given the approach, so run the different steps involved in succession and are changing how variable to finally come to the conclusion.

The same sorting algorithm can be formulated in the declarative programming language Haskell as follows:

Quicksort [ ] = []   quicksort (x: xs ) = quicksort [ n | n < -xs, n = x] The programmer describes what the program does with a command, so how to deal with what input, the calculation process is of no interest. The calculations are then carried out by manipulating values ​​. Main control structure is recursion, for reasons of efficiency especially the tail recursion.

Benefits

  • The programs are often shorter than comparable imperative programs.
  • Evidence (eg, proof of correctness, proofs of program properties ) are thanks to simpler mathematical basis more feasible, if at all possible ( inter alia, lambda calculus ).
  • There are no side effects because of referential transparency. Programs are thus partially evaluated, thus allowing for example, the treatment of infinite data structures.

Disadvantages

  • Performance: The specified quicksort algorithm runs much faster than in Haskell in Pascal and is therefore better suited for processing large amounts of data.
  • Declarative programming paradigms are particularly imperative and object -oriented paradigms according to their acceptability. One likes to talk of so-called academics languages.
226311
de