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

  Create an essay describing the maintenance mode

Create an essay describing the Maintenance Mode. Include each of the model's five domains and explain why each domain is important

  Write a program for user registration

Write a program for user registration: First the program tells the user "enter 1 to register, enter 2 to sign in, enter 3 to quit.

  Write a subprogram that stores values into two arrays

write a subprogram that stores values into two arrays.One of the arrays will hold printable ASCII characters and the other will hold associated integer values.

  Write another model program for the newsreader

Write another model program, implementation, and stepper for the newsreader, where each action returns value of all three state variables as C# out parameters.

  Write a java application to to ask the user to input

Write a Java application to to ask the user to input an even number between -100 to 100.

  Explain how a stop button must always be wired and why

Explain two advantages of wiring the holding contacts to an input module and then programming the holding contact address into the PLC program.

  A program that reads in an unspecified number of integers

The assignment number, your name, StudentID, Lecture number(time), and a class description need to be included at the top of each file/class.

  What do the different colors mean in the wireshark log

This is a flexible and powerful tool. Any network administrator worth his or her salt will know how to run Wireshark. Most professionals use it often.

  What is a virtual community

What is a virtual community? Give an example of a mobile application that would lend itself to use in a virtual community.

  A constructor that has an const unsigned int parameter

A constructor that has an const unsigned int parameter. This constructor needs to dynamically allocate an array of the size passed into the parameter.

  Calculate the length of the hypotenuse of a right triangle

Write a function that will calculate the length of the hypotenuse of a right triangle and add it to the area.

  What are the potential benefits to consumers

What are the implications for firms operating in effected industries? What are the potential benefits to consumers? Do consumers lose anything from this legislation? Include the urls of your sources

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