In: Computer Science
Question 1: Discuss the concept of lazy evaluation and why this is a powerful advantage of functional programs.
Question 2: Describe and Discuss the concepts of Higher order functions and currying in a functional programming language such as Haskell (or Standard ML) and describe why these concepts are important.
1) Lazy evaluation :
Lazy evaluation is the evaluation of an expression when it is needed. It do not repeat the evaluation of same expression for many times. The expression is evaluated whenever it needs the value otherwise that expression is ignored. It executes the expression only if it is required for final output otherwise it is ignored.It discards the unused functions or expressions.
The powerful advantage of this lazy evaluation in functional programs is reduce the time complexity and increases the performance of the System . Time complexity is reduced because unuseful functions are ignored and not executed. Therefore system works fastly and give outputs fastly.
2) Higher order functions and currying :
Higher order functions :
For example,
func1(int a,int b){ c= a+b;}
func2(int d, int e) { f = d-e; }
func(func1,func2) { //statements;//} //func is a higher order function.
Currying :
For example let consider adding two numbers
Normal function without currying :
Addtwonumber (a,b)
{
return (a+b);
}
With Currying :
Addnumberone(a) -//Function with single parameter
{
return addnumbertwo(b) //-Function with single parameter
{
return (a+b);
}
}
These concepts are important in functional programming languages, because it takes different data types from different functions and it runs the function in sequential order clearly. These are important in taking multiple parameters which is not possible in normal functions of Haskell Programming languages. Therefore they are very important in functional programming languages.