Haskell – Command programming vs declarative programming

Haskell

Preface

There is a saying about these two concepts: imperative programming (Imperative programming) emphasis on what we do how, and declarative programming (Declarative programming) emphasize what we do what.

Can take a practical example as follows, a couple comes to the restaurant, the waiter wants to ask where they sit, they will answer:

  • Command direction (Imperative, how): I see the other table is empty and close to the window, the place has a nice view, so my husband and I will sit there.
  • Declaration direction (Declarative, what): Give me a table of 2.

We may notice that when giving the declaration, there is usually a hidden imperative. For example, when calling a table with 2 people, this couple assumed that the waiter knew which table to choose and how to guide their guests to that table.

SQL and HTML examples

SELECT * FROM Users WHERE Country='Vietnam';
<article>
    <header>
        <h1>Declarative Programming</h1>
    </header>
</article>

As we can see, in both languages ​​we are more concerned with what will be output what, don’t care about them how to produce that result.

Declared programming vs functional programming

Functional programming (Functional programming) Can be considered a sub-branch of declarative programming, in functional programming, we also give the problem to the computer to solve. (the what), but at the same time also give a solution (like how), it’s just that we don’t do anything to change the value / state of the variables.

We will familiarize ourselves with this model in the following sections.

Typical language of each type

  • Command language (Imperative): Fortran, Algol, Pascal, C / C ++, Java
  • Declaration language (Declarative):
    • Logic language: Prolog
    • Functional language (Functional): LISP, APL, ML, FP, Haskell, F #

Hope this helps!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *