CSC/ECE 517 Fall 2010/ch1 1e az

From Expertiza_Wiki
Revision as of 23:54, 8 September 2010 by Avenkat4 (talk | contribs)
Jump to navigation Jump to search

Programming Paradigms

Every computer program needs a style of writing which specifies how to solve a software engineering problem. This style is represented by the paradigm. Each computer program follows one or more paradigm which differs in representing the elements of a program(such as variables and objects) and the steps needed to compute a task.

Diferent paradigms are:

1. Procedural/imperative paradigms: Assembly, C, C++, Java, C#

2. Object Oriented paradigm : C++, Java, Python, Ruby, Scala, C#

3. Functional Paradigm : Lisp, Haskell, Clojure, Scala, OCaml, Ruby

4. Logic Paradigm: Prolog


MultiParadigm Programming

Multiparadigm refers to use of a combination of programming paradigms for solving a computer problem. Some languages subscribe strictly to a single paradigm like Assembly and C. Others like Java, C++, Scala and C# employ more than one paradigm. Every paradigm comes with its own strength and weakness and this quite motivates us to take advantage of each paradigm and use it in a manner that best fits the problem at hand.


Overview of Functional Programming

Functional programming is derived from lambda calculus which models computations as the evaluation of functions and recursions. More emphasis is laid on application of functions rather than changing the state of variables in a program. Functional programming relies heavily on recursions and it is the only way to iterate instead of loops.

How it differs from imperative programming?

Imperative programming follows the Von-Nuemann architecture and mainly consists of loops and usage of globa states to perform a computation. For example consider the task of computing the sum of numbers from 1 to n. In imperative style,

       sum := 0         // global state
       for i<- 1 to n do
           sum := sum + i
           

In Functional style,

         func sum(n)        // note that sum is a function and is recursive
             if  n = 1 then
                 return 1
            else
                return n + sum(n-1)
                

Pure and impure functional programming

Purely functional programming exhibit referential transparency which does not involve any global state or I/O changes. If the same functional expression results in the same output value for the same argument x at different stages of execution , then the function is said to be pure which is devoid of any global state change. For example Haskell is purely functional.