Here is the difference:
Imperative:
- Start
- Turn on your shoes size 9 1/2.
- Make room in your pocket to keep an array[7] of keys.
- Put the keys in the room for the keys in the pocket.
- Enter garage.
- Open garage.
- Enter Car.
... and so on and on ...
- Put the milk in the refrigerator.
- Stop.
Declarative, whereof functional is a subcategory:
- Milk is a healthy drink, unless you have problems digesting lactose.
- Usually, one stores milk in a refrigerator.
- A refrigerator is a box that keeps the things in it cool.
- A store is a place where items are sold.
- By "selling" we mean the exchange of things for money.
- Also, the exchange of money for things is called "buying".
... and so on and on ...
- Make sure we have milk in the refrigerator (when we need it - for lazy functional languages).
Summary: In imperative languages you tell the computer how to change bits, bytes and words in it's memory and in what order. In functional ones, we tell the computer what things, actions etc. are. For example, we say that the factorial of 0 is 1, and the factorial of every other natural number is the product of that number and the factorial of its predecessor. We don't say: To compute the factorial of n, reserve a memory region and store 1 there, then multiply the number in that memory region with the numbers 2 to n and store the result at the same place, and at the end, the memory region will contain the factorial.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…