Write a function that computes the sum of cubes

Assignment Help Computer Engineering
Reference no: EM131234079

I - Functional Programming - Do 1, 2, 5 to 10

To begin:

  • In Eclipse create a Scala project called FunctionLab.
  • Add a worksheet to this project called session
  • Add a Scala interpreter to this project

In the session worksheets define and test the following functions. Your implementations should be as concise as possible.

To end:

  • Export session.sc to your file system and send it to the grader by the deadline.

1. Problem

Re-implement compose so that it's as generic as possible. (I.e., it will compose and two "compatible" functions.)

2. Problem

Implement the self-composition iterator function:

defselfIter[T](f: T=>T, n: Int) = f composed with itself n times.

Test your function with:

definc(x: Double) = x + 1

defdouble(x: Double) = 2 * x

Note:

selfIter(f, 0)= id where id(x) = x

3. Problem

Write a function called countPass that counts the number of elements in an array of elements of type T that pass a test of type T=>Boolean

4. Problem (Objects as functions)

Objects can be represented by message dispatcher functions:

defdispatch(msg: String): String = {execute msg}

A constructor creates "objects":

defmakeAccount(initBalance: Double = 0.0) = {

   var balance = initBalance

   var delegate: (String)=>String = (y: String) => "error"

   def dispatch(msg: String)(amt: Double): String = {...}

   dispatch _

}

Here's how the constructor is used:

val savings = makeAccount(100)

val checking = makeAccount(200)

println(savings("withdraw")(30))  // prints $70

println(checking("withdraw")(30)) // prints $170

println(checking("transfer")(30)) // prints error

Complete and test makeAccount.

5. Problem: Control Loop

Find a tail recursive implementation of controlLoop.

6. Problem: Population Growth

A pond of amoebas reproduces asexually until the pond's carrying capacity is reached. More specifically, the initial population of one doubles every week until it exceeds 105. Use your controlLoop function to compute the size of the final population.

7. Problem: Finding Roots of Functions

Newton devised an algorithm for approximating the roots of an arbitrary differential function, f, by iterating:

guess = guess - f(guess)/f'(guess)

Recall that f'(x) is the limit as delta approaches zero of:

(f(x + delta) - f(x))/delta

Use Newton's method and your controlLoop to complete:

defsolve(f: Double=>Double) = r where |f(r)| <= delta

8. Problem: Approximating Square Roots

Use your solve function to complete:

defsquareRoot(x: Double) = solve(???)

9. Problem: Approximating Cube Roots

Use your solve function to complete:

defcubeRoot(x: Double) = solve(???)

10. Problem: Approximating Nth Roots

Use your solve function to complete:

defnthRoot(x: Double, n: Int) = r where |rn - x | <= delta

II - List Processing - Do 6, 7 and 8

To begin:

  • In Eclipse create a Scala project called ListLab.
  • Add a worksheet to this project called session
  • Add a Scala interpreter to this project

In the session worksheet define and test the following functions.

For each function implement four versions:

  • Iterative version
  • Recursive version
  • Tail-recursive version (this should be different from the previous version)
  • map-filter-reduce version

All of your implementations should be as generic as possible.

To end:

  • Export session.sc to your file system and send it to the grader by the deadline.

1. Problem

Write a function that computes the sum of cubes of all odd numbers occurring in a list of integers.

2. Problem

Write a function that computes the sum of numbers in a list of lists of numbers:

sumOfSums(List(List 1, 2, 3), List(4, 5, 6)) = 21

3. Problem

Write a function that returns the depth of a list of nested lists:

depth(List(List(List 1, 2, List(3)))) = 4

4. Problem

Write a function that computes the average of a list of doubles

5. Problem

Write a function that returns the largest element of a list of comparables.

6. Problem

Write a function that returns the number of elements in a list that satisfy a given predicate. (The predicate is a parameter of type T=>Boolean.)

7. Problem

Write a function that returns true if all elements in a list satisfy a given predicate.

8. Problem

Write a function that returns true if any element in a list satisfies a given predicate.

9. Problem

Write a function that reverses the elements in a list.

10. Problem

Write a function that returns true if a given list of comparables is sorted.

11. Problem: My Map

If the List.map function didn't exist, how would you define it?

12. Problem: Take, Drop and Zip

If take, drop, and zip didn't exist for lists, how would you define them?

13. Problem: Streams

A stream is like a list except that it is constructed using the lazy version of cons:

scala>  val s1 = 1 #:: 2 #:: 3 #:: Stream.Empty

s1: scala.collection.immutable.Stream[Int] = Stream(1, ?)

scala> s1.head

res0: Int = 1

scala> s1.tail.head

res1: Int = 2

Create the following streams

  • An infinitely long stream of 1's
  • The stream of all non-negative integers
  • The stream of all non-negative even integers
  • The stream of all squares of integers

14. Problem: Proplog and Logic Programming

A logic program consists of a set of rules and facts:

Conclusion if Condition1 and Condition2 and ...

A fact is simply a conclusion without conditions.

15. Problem

Find an iterative implementation of solve

16. Problem

Find a non-recursive, non-iterative implementation of solve that uses map. filter, and reduce.

III - Recursion - Do 5, 6, 9 and 10

To begin:

  • In Eclipse create a Scala project called RecursionLab.
  • Add a worksheet to this project called recursionSession
  • Add a Scala interpreter to this project

In the session worksheet defines and test the following functions. Your implementations should be recursive.

To end:

  • Export session.sc to your file system and send it to the grader by the deadline.

Enter the following definitions into the beginning of your session:

def inc(n: Int) = n + 1

def dec(n: Int) = n - 1

1. Problem

Re-define the function:

add(n: Int, m: Int) = n + m

Your definition should only use recursion, inc, and dec.

2. Problem

Re-define the function:

mul(n: Int, m: Int) = n * m

Your definition should only use recursion, add, inc, and dec.

3. Problem

Re-define the function:

exp2(m: Int) = pow(2, m) // = 2^m

Your definition should only use recursion, add, mul, inc, and dec.

4. Problem

Define the hyper-exponentiation function:

hyperExp(m: Int) = exp(exp(... (exp(1)) ...)) // n-times

Your definition should only use recursion, exp2, add, mul, inc, and dec.

Notes:

  • This will probably cause a stack overflow each time it's called.
  • hyperExp(0) = 2

5. Problem

Re-implement all of the above functions as tail recursions. Does that improve the stack overflow problem? What about the computation time, is that improved?

6. Problem

At age six Friedrich Gauss discovered an algorithm for tri that uses O(1) space and time! What is it?

7. Problem

Reimplement and test the repl procedure in Calculator.scala without using iteration. Your solution should avoid stack overflow errors.

8. Problem

Implement the following functions using only the solutions for #5.

hyper2Exp(m: Int) = hyperExp(hyperExp(... hyperExp(0)...)) // m-times

hyper3Exp(m: Int) = hyper2Exp(hyper2Exp(... hyper2Exp(0)...)) // m-times

ackermann(N, m) = hyperNExp(m)

9. Problem

Find a recursive and tail recursive implementations of the Fibonacci function.

10. Problem

Find a recursive implementation of:

choose(n, m) = # of ways to choose m things from n

Note: n and m are non-negative integers.

Hint: Pick a special item from the set. Call it x. Then add the number of choices containing x and the number that don't.

11. Problem

Assume two teams, A and B are playing a tournament. The first team that wins k games wins. Assume the probability that either team wins one game is 0.5. Find a recursive implementation of:

probability(n, m) = the probability A wins the tournament assuming A must win n more games and B must win m more games.

Attachment:- Assignment.rar

Reference no: EM131234079

Questions Cloud

How can team members get back on track : When communication breaks down between members; when decisions need to be made quickly; when infighting or turf wars threaten a project, how can team members get back on track?
What do you believe the value of this firm to be : You are valuing a firm with a "pro forma" . - Assume that the appropriate discount rate for a firm of this riskiness is 12%. - What do you believe the value of this firm to be?
Describe a work or personal experience : Describe a work or personal experience that relates to Learning to listen, not just speaking.  If you have not dealt with a global negotiation, you may locate an article that deals with one of these techniques and describe it.
Write a function that computes the sum of cubes : Write a function that computes the sum of cubes of all odd numbers occurring in a list of integers. Write a function that computes the sum of numbers in a list of lists of numbers: sumOfSums(List(List 1, 2, 3), List(4, 5, 6)) = 21
Function that returns a stringrepresenting its intargument : Write an input function that reads such strings as fast as you can think of. You can choose the interface to that function to optimize for speed rather than for convenience. Compare the result to your implementation's >> for strings.
Should you sell or lease your building : You can sell your building for $200,000. - Your cost of capital is 0.5% per month. Should you sell or lease your building?
What is meant by measures of effectiveness : What is meant by "measures of effectiveness"? For the effectiveness analysis of a sport utility vehicle (SUV), list what you think would be the eight most important characteristics that should be exercised and measured in the analysis.
Explain any information that may help alliance reduce costs : Describe any information that may help Alliance reduce costs while providing better service. Propose a new approach that could be used by Alliance by using the purchase information that can be obtained on individual customers.

Reviews

len1234079

10/7/2016 6:59:54 AM

In this assignment do problem 5, 6, 9, and 10 in Recursion. Do problem 1, 2 , 5 to 10 in function programming. And do problem 6, 7, 8 in list processing.

Write a Review

Computer Engineering Questions & Answers

  Mathematics in computing

Binary search tree, and postorder and preorder traversal Determine the shortest path in Graph

  Ict governance

ICT is defined as the term of Information and communication technologies, it is diverse set of technical tools and resources used by the government agencies to communicate and produce, circulate, store, and manage all information.

  Implementation of memory management

Assignment covers the following eight topics and explore the implementation of memory management, processes and threads.

  Realize business and organizational data storage

Realize business and organizational data storage and fast access times are much more important than they have ever been. Compare and contrast magnetic tapes, magnetic disks, optical discs

  What is the protocol overhead

What are the advantages of using a compiled language over an interpreted one? Under what circumstances would you select to use an interpreted language?

  Implementation of memory management

Paper describes about memory management. How memory is used in executing programs and its critical support for applications.

  Define open and closed loop control systems

Define open and closed loop cotrol systems.Explain difference between time varying and time invariant control system wth suitable example.

  Prepare a proposal to deploy windows server

Prepare a proposal to deploy Windows Server onto an existing network based on the provided scenario.

  Security policy document project

Analyze security requirements and develop a security policy

  Write a procedure that produces independent stack objects

Write a procedure (make-stack) that produces independent stack objects, using a message-passing style, e.g.

  Define a suitable functional unit

Define a suitable functional unit for a comparative study between two different types of paint.

  Calculate yield to maturity and bond prices

Calculate yield to maturity (YTM) and bond prices

Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd