Question

In: Computer Science

Question 1: Discuss the concept of lazy evaluation and why this is a powerful advantage of...

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.

Solutions

Expert Solution

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 :

  • Higher order function is a function which takes functions as a parameter and returns the function as a result.
  • Normal function contains parameters and done it's operations defined.
  • But when a function takes other functions as parameters it is said to be "Higher order function".
  • And it also said to be when it returns the function as a result.

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 :

  • Currying is a technique used in a functional programming languages which takes functions as a parameters and return functions as a result.
  • Functional programming languages like Haskell, functions take mostly single parameter only. It is not possible to take multiple parameters at a time.
  • To overcome this , currying is the methodology used to run multiple parameters in Haskell.
  • Currying , each function take single parameter only and after completion of that function it is combined with next sequence function parameter. So like this functions are used in sequence to take multiple parameters.
  • The last function in currying will gives the output.

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.


Related Solutions

1. Discuss the concept of comparative advantage. Explain and give an example. 2. Identify the factors...
1. Discuss the concept of comparative advantage. Explain and give an example. 2. Identify the factors that affect demand. Come up with your own example to illustrate how these factors shift demand (do not use examples from the textbook).
explain why the concept of competitive advantage is central to the study of strategic management
explain why the concept of competitive advantage is central to the study of strategic management
Discuss why might it be important to separate the evaluation of the performance of a foreign...
Discuss why might it be important to separate the evaluation of the performance of a foreign subsidiary from that of its manager, including items such as controllable and uncontrollable items of the parent company, host government or others.
a) Discuss four (4) reasons why training evaluation is important.
a) Discuss four (4) reasons why training evaluation is important.
Discuss examples of 1) elasticity, including an explanation of why or how they demonstrate the concept...
Discuss examples of 1) elasticity, including an explanation of why or how they demonstrate the concept of elasticity; and 2) examples of externalities, again including an explanation of why or how they demonstrate the concept of externalities.
Discuss examples of 1) elasticity, including an explanation of why or how they demonstrate the concept...
Discuss examples of 1) elasticity, including an explanation of why or how they demonstrate the concept of elasticity; and 2) examples of externalities, again including an explanation of why or how they demonstrate the concept of externalities.
1) How does information and knowledge management link to competitive advantage? Discuss the reasons why or...
1) How does information and knowledge management link to competitive advantage? Discuss the reasons why or why not a dominant firm might or might not consider attacking smaller competitors to increase market share? 2) Companies have to update their strategy on a regular basis but sometimes need to look at radical change, briefly explain these two types of Strategic Change and how they might affect company organization. Define incremental and transformational change. enter academic citations.
Discuss why an organization might prefer job evaluation or market pricing.
Discuss why an organization might prefer job evaluation or market pricing.
Discuss why the concept of full disclosure is difficult to apply.
Discuss why the concept of full disclosure is difficult to apply.
Correct answer is given, please explain why for each. Concept Question 1 – A rock is...
Correct answer is given, please explain why for each. Concept Question 1 – A rock is thrown straight up from the surface of the earth. Ignoring air resistance, which of the following statements is correct as the rock moves upward? a) The total energy of the rock increases. b) Both the kinetic energy and the gravitational potential energy of the rock remain the same. c) Both the gravitational potential energy and the total energy of the rock increase. d) Both...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT