Programming Paradigms
Declarative language:
What not how :
· Describe what should happen, not how to do it.
· The language should figure out how based upon the truths declared.
· No side effects: A rule is true And a function gives the exact same response regardless of the state of the system.
· No concept of mutable variables: In Excel, Cell A2 names a value, not a container to hold a value that changes.
· No sequence of commands. Express data flow, not command sequence. Example: Excel formula: =A1 + A2. (Use recursion, based on statements of facts, to flow.)
· Types:
· logic programming: Describes relationships in terms of inference rules.
o Example: Prolog
· functional programming: Describes relationships in terms of functions.
o Example: Haskell , Excel, and a subset of Racket.
Imperative: How not what:
· Exact rules for manipulation of the state of a computer system and its variables. Imperative does not have to consider the order or how it will be done. Think of commands to make something happen.
o Ex of pure imperative: SQL insert / update / delete
· Types:
o Procedural: Exactly how algorithms: Describe the exact steps to perform (using procedures or functions). Involves changing variables. First do this and next do that.
§ Example : C
o Object Oriented: Manipulates objects through predefined methods. Define objects in classes. Changes state of variables. Send messages between objects.
§ Example : C++, Java
Racket strengths:
· Adding language syntax: powerful macros allow programmer to add syntax constructs to extend the language
· continuations: allow you to capture the future value of a computation and store or pass it.
· dynamic typing: let the program figure out what type of variable is held. Saves program time.
· Manages its own memory.
· function on the fly (lambda): Easily create a function on the fly and pass it around. (Java allows, but not easily)
· function closure: a function created on the fly (lambda) can refer to variables in the scope of its creation. (Java limits the variables an on the fly function can reach.)
Great explanation lecture: http://www.infoq.com/presentations/Taming-Effect-Simon-Peyton-Jones
from MSDN: http://msdn.microsoft.com/en-us/library/bb669144.aspx
Characteristic |
Imperative
approach |
Functional
approach |
Programmer focus |
How to perform tasks (algorithms)
and how to track changes in state. |
What information is desired and
what transformations are required. |
State changes |
Important. |
Non-existent. |
Order of execution |
Important. |
Low importance. |
Primary flow control |
Loops, conditionals, and function
(method) calls. |
Function calls, including
recursion. |
Primary manipulation unit |
Instances of structures or
classes. |
Functions as first-class objects
and data collections. |
Sources:
http://foldoc.org/declarative+language
http://foldoc.org/imperative+languages
http://foldoc.org/procedural+language
http://people.cs.aau.dk/~normark/prog3-03/html/notes/paradigms_themes-paradigm-overview-section.html