Compare and Contrast Functional and Imperative Programming Languages (10 Marks)
Compare and Contrast Functional and Imperative Programming Languages (10 Marks)
example of an expression
project x (a,b,c) = (a, x,(projectDataColumns x (a,b,c)))
example of an statement
p += 15;
example of a closure in haskell
f x = (\y -> x + y)
Imperative approach
for($i=1;$i<=12;$i++)
{
for($j=1;$j<=12;$j++)
{
$string .= $i*$j;
}
}
The above code calculates the answer to the multiplication table from 1 to 12. In Haskell
this code can be implemented using list comprehensions illustrated below.
[x*y|x<-[1..12],y<-[1..12]]
Double::Int->Int
Double x = x* 2
The prelude function Map. can take this function as a parameter as illustrated below
xs = [ 1,3,5,6]
This applies the double function to each element of the list xs. The function Map takes
function Double as a parameter.
Pure Functions, these functions are functions that have no side effects, i.e they will only
return values not yield other actions.(modify state).
for example a function a function that changes degree Celsius to degree Fahrenheit is a
pure function, and conversion of exchange rate, MYR to USD is an impure function, it
depends on a lot of external factors, i.e, the answer can be different time to time.
Lazy Evaluation, evaluation takes place only when required. It will only evaluate an
argument to a function if that's arguments value is needed to compute the overall result. If
the argument is structured, i.e if its a tuple or alist, it will only evaluate those parts that
are needed.
here the only the first part sqrt(16) get evaluated, the other part is ignored.
Currying, This is transforming a function that takes multiple arguments in such a way that
it can be called with a chain of functions each with a single argument. An non-curried
function's arguments would probably contain tuples.
Multiply m n = m*n
The curried version can accept one argument and return a function. this function is a higher
order function.
Referential Transparency, this means the function always evaluates the same no matter
what the context is. There are no side-effects and it does not depend on the state.
for example the mathematical function sin(x) is transparent, i.e it returns the same value
for sin(x) no matter what the context is. however the expression x++; ( x = x+1) is not
transparent because it alters the value in variable x.
Explain The term Tuple in context of
functional Programming paradigm, Give 3
examples? (5 marks)
Tuples is a way to storing multiple values in a single value. But it requires you to know
exactly how many values are to be stored in the tuple. In functional programming
languages tuples are useful when we want to return multiple values from a function, or it a
can even be used as a primitive data type.
Cordinates :: Int->Int->(Int,Int)
Cordinates x y = (x,y)
Characteristics.
Modularity, Procedural languages allows the code to be modular, a function that is used to
perform a certain task can be used over and over again with out repeating the code.
Modules ensure separation of concerns, this improves maintainability and future expansion.
Having the codes in modules makes it really easy to manage, and encourages code reuse,
same code can be implemented in multiple projects via libraries.
Scoping , in specially large systems, involving a lot of variables being passed around in
functions, scoping can be very helpful, in ensures that the variables used in function stays
within that function. procedures cant use variable from other procedures or another
instance of the same procedure without explicit permission.
//code
var variable1
function A{
var variabe2;
function C{}
}
function B {
In the code illustrated above, variable1 is a global variable accessible to all functions, bu
variable2 can only be accessed by function A and C. Similarly function C is not available to
function B.
Benefits
Simplicity , Procedural languages are simple compared to declarative, they offer a clear
and concise solution, specially when considering simple tasks, it doesn't force you to create
objects or classes to do a simple task ( as some object oriented languages like java do).
Efficiency, procedural languages are more efficiently translated into machine language,
and they run more efficiently because the level of abstraction is low, in other programming
paradigms like declarative the level of abstraction is very high, its more similar to a natural
language which translates less efficiently into machine code.
Bad, because it offers comparatively less documentation, specially if its a large project. This
can also help catch errors at compile time.
Minimize side effects, a declarative language tries to minimize or eliminate side effects
completely, it accomplishes it by describing what to do, instead on describing how to go
about doing this task, which is what an imperative language would do by implementing a
detail description of the algorithm.
Single Assignment, in declarative languages variables can only have one value assigned
to them and this cannot be altered during program execution. This is called non-destructive
assignment. This encourages the use of recursive techniques.
Program flow, the flow of the program is not determined by control structures, it is left to
the language it self. This minimizes logic errors that may occur if a programmer decided to
implement this. This is true for other programming paradigms like imperative languages.
Further more order of how execution of code is irrelevant.
Natural Language Processing, Prolog is very suitable to parse and do natural language
processing. It is used to create spelling correctors or applications that seek out grammetical
errors.
Pattern matching,