Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between functional programming and declarative programming?

I would like understand the difference between functional, and declarative programming.

Can you show me an example where the code is declarative, but not yet functional?
Is it possible to be functional but not declarative, i.e. imperative?

like image 495
gabox01 Avatar asked Jan 22 '14 11:01

gabox01


People also ask

What is the difference between declarative and functional programming?

Functional Programming is a declarative programming paradigm, in contrast to imperative programming paradigms. Declarative programming is a paradigm describing WHAT the program does, without explicitly specifying its control flow.

What is the difference between functional programming and procedural programming?

Procedural programming uses a very detailed list of instructions to tell the computer what to do step by step. This approach uses iteration to repeat a series of steps as often as needed. Functional programming is an approach to problem solving that treats every computation as a mathematical function.

How is functional programming declarative?

Functional programming languages are declarative, meaning that a computation's logic is expressed without describing its control flow. In declarative programming, there are no statements. Instead, programmers use expressions to tell the computer what needs to be done, but not how to accomplish the task.

Is functional programming always declarative?

No. Functional programming is not per se the same as declarative programming and vice versa. Prolog - which is logical programming - is by many considered to be a declarative programming language as well.


1 Answers

A non-functional declarative language is PROLOG. Programming in PROLOG is stating a number of facts, and then ask questions, which the system tries to verify or deny.

Example:

human(socrates).       // "Socrates is a human."
mortal(X) :- human(X). // "If X is a human, then X is mortal" or
                       // "All humans are mortal."

? mortal(socrates)     // Is Socrates mortal?
Yes.
? mortal(X)            // Who is mortal?
socrates               
? mortal(pythagoras). 
No.                    // since system doesn't know about any human except Socrates

Another well known language that is declarative, but not functional, is SQL.

Note that there are not only no functions as first class values. In the PROLOG example, there are no functions at all! To be sure, both SQL and PROLOG have some built-in functions, but have no way to let you write your own functions. One could think that the rule

mortal(X) :- human(X).

is a function, but it isn't, it is an inference rule. Hence, declarative, non-functional languages.

For the second part of your question: it is certainly possible to write imperative code in functional programming languages. Simon Peyton Jones once stated that he thinks that Haskell is the finest imperative programming language in the world. (And this was only a half joke.)

Example:

 main = do
      print "Enter a number"
      line <- getLine
      print (succ (read line :: Int))
like image 162
Ingo Avatar answered Oct 02 '22 17:10

Ingo