Write non-tail-recursive and other tail-recursive function

Assignment Help Computer Engineering
Reference no: EM132233143

PROJECT

The purpose of this project is for you to continue writing OCaml functions, related mostly to tail recursion.

1. Write two versions, one non-tail-recursive and the other tail-recursive, of the following function.

The function takes as arguments an element x (of any type) and a non-negative integer n, and returns a list of n entries all identical to x. If the function is called with a negative n, an exception is thrown.

For example, replicate x 5;; would produce [x;x;x;x;x].

2. Write two versions, one non-tail-recursive and the other tail-recursive, of the following function.

The function takes as argument a non-negative integer n, and returns the list of increasing positive integers up to n (empty list if n is 0). If the function is called with a negative n, an exception is thrown.

For example, makeList 4;; would produce [1;2;3;4].

3. Write two versions, one non-tail-recursive and the other tail-recursive, of the following function.

The function takes as argument a list, and returns the reversed list. For example, reverse [6;8;2;5];; would produce [5;2;8;6].

4. Complete the following code for the function that takes an integer n as argument, and that returns the list of (prime) divisors for n.
For example, divisors 120;; would produce [2;2;2;3;5].

let divisors n =
if n < 2 then raise (Failure "Number must be > 1")
else
let rec helper testDivisor currentDividend =
if currentDividend = 1 then []
else


5. Complete the following code for the function that takes an integer n as argument, and that returns true or false according to whether n is a prime number or not.

Recall that a number p cannot divide n if p*p > n.

For example, isprime 17;; would produce true, while isprime 20;; would produce false.

let isprime n =
if n < 2 then raise (Failure "Number must be > 1")
else
let rec nHasNoDivisor p =

Note: Do run test your functions in the OCaml toplevel, in your Ubuntu.

Attachment:- assignment.rar

Verified Expert

The solution file is prepared in ocaml programming language and implemented following programs are: Print pattern for given number using non tail recursion and recursion function Print numbers from 1 to given number using non tail recursion and recursion function Program to print reverse of the list using non tail recursion and recursion function Display the prime factors of given number Check given number is prime number or not. The solution files are combined as single file with extension of .ml and screen shots of all programs are attached separately .

Reference no: EM132233143

Questions Cloud

Creation packages like endnote-refworks : Create bibliography for two articles using CiteThisForMe or another relevant reference creation packages like EndNote, RefWorks, ProCite and Reference Manage.
Make expression analyser which compile arithmetic expression : CE305 Assignment - Expression Analyser. build an expression analyser which compiles arithmetic expressions into a form that can be evaluated
News agency due to downturn in magazine sales : This arrangement works well for four (4) months until Ned is unexpectedly dismissed from his job at the news agency due to a downturn in magazine sales.
Substitutes for commodity which forms major raw material : Looking for substitutes for a commodity which forms a major raw material in the goods manufactured by the company is a correct business decision;
Write non-tail-recursive and other tail-recursive function : Write two versions, one non-tail-recursive and the other tail-recursive - Complete the code for the function that takes an integer n as argument
Identify local non-profit organization-agency to profile : Identify a local non-profit organization/agency to profile. What is their mission statement? What does their organizational chart suggest about their structure?
Prevent disastrous transfer of power int business : What should a small family owned business do to prevent a disastrous transfer of power int the business?
Distinguish between forecasting time frames : Identify uses of demand forecast. Distinguish between forecasting time frames. Describe common features of forecast
Compare performance of the two methods : Compare the performance of the two methods by using the mean squared error as the performance criterion, with error measurement beginning in April.

Reviews

len2233143

2/12/2019 10:42:37 PM

Submit an archive zip file of the single .ml (OCaml) file containing the required phrases for the problems specified below. Do not submit multiple files, even if you develop your solutions one at a time (copy/paste everything onto a single file). You need to zip this .ml file to submit, because Blackboard might create all kinds of problems with filename extensions it does not like: you may think it goes through, but we do not receive it.

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