Write a function prime that takes a single integer

Assignment Help Programming Languages
Reference no: EM13187527

Sorting a list is a common example in Computer Science, functional languages make it very easy to express a sorting algorithm. There are a number of ways a list can be sorted, ascending order, descending order, the first/second value of a tuple etc.

a) Write a higher order function that takes as its parameters a function that will be used to compare two elements and a list to be sorted. The result of the function should be a list that has been sorted according to the comparison function. (You may define that the items in the list will be Ord a, that means you can use comparison operator '==' to compare two items). Some example output expected of this function is shown below:

Main>mySort (<) [1,5,3,6,4,1,3,3,2]

[1,1,2,3,3,3,4,5,6]

Main>mySort (>) [1,5,3,6,4,1,3,3,2]

[6,5,4,3,3,3,2,1,1]

Main>mySortlongerWord ["Hello", "The", "a", "Daniel", "Declarative"]

["Declarative", "Daniel", "Hello", "The", "a"]

b) Write a higher order function list To that takes a function f and a number n and uses list comprehension to return a list of all the numbers from 1 to n for which the function f evaluates to True.

A prime number is a number that is only divisible by itself and 1. For example the number 7 is divisible by 7 and 1 but it cannot be divided by anything else.

c) Write a function prime that takes a single integer and returns true if it is a prime number and false if it is not. Your functions should give useful error messages if 0 or negative values are given as parameters. Use currying to define a function prime To in terms of the function list To. This function should return a list of all the prime numbers up to a given value n.

Some expected output is shown below:

Main> prime 1

True

Main> prime 4

False

Main>primeTo 10

[1,2,3,5,7]

d) Write a Haskell script that will

1. Store an unsorted list of students including their:

? First and last names

? Student ID

? midterm mark

? final exam mark

? assignment 1 mark

? assignment 2 mark

? assignment 3 mark

2. Sort the list according to the last name

3. Calculate the students final grade based on the following formula:

final grade = midterm * 0.25 + final * 0.5 + ((assignment 1 + assignment 2 + assignment3) / 3.0) * 0.25

4. Print a table with proper headings containing the following information for each student.

Student ID First Name Last Name Final Grade

5. Print a footer containing the class average at the bottom of the table.

Some expected output is shown below:

Computer Science Declarative Programming Results

Student ID First Name Last Name Final Grade

01234567 Jo Bloggs A

12345678 John Doe B

23456789 Jane Doe C

. . . .. . . Class Average: B

Reference no: EM13187527

Questions Cloud

Write a linear equation giving the value v of the equipment : A contractor purchases a new bulldozer for $45,000. After 15 years the bulldozer will be outdated and have no value. Write a linear equation giving the value V of the equipment during the 15 years it will be used, where t is the number of years af..
State systematic treatment of equilibrium : weak base so you must consider other equilibria. (Ksp(ZnC2O4)=7.5X10^-9, Kb1=1.8X10^-10, Kb2=1.8X10^-13). Solve using systematic treatment of equilibrium.
At what price should the manufacturer sell the lamps : The manufacturer can produce the lamps at a cost of $4 per lamp. At what price should the manufacturer sell the lamps to generate the greatest possible profit?
How long ago did you deposit the 500 dollar : Some time ago you put $500 into a bank account for a "rainy day". Since then, the bank has been paying you 1% per month, compounded monthly. Today, you checked the balance and found it to be $708.31. How long ago did you deposit the $500
Write a function prime that takes a single integer : Write a higher order function list To that takes a function f and a number n and uses list comprehension to return a list of all the numbers from 1 to n for which the function f evaluates to True.
State how many miligrams of concentrated sulfuric acid : how many miligrams of concentrated sulfuric acid (H2SO4) [having the strength is 18 M], are required to prepare 750 ml of 3.0 M
What fraction of the substance will remain after 105 days : Suppose that you possess some of a radioactive substance that has a half-life of 21 days. What fraction of the substance will remain after 105 days?
State the number of the enzyme molecules inside each cell : Estimate the approximate concentration of a typical enzyme and the number of the enzyme molecules inside each cell. Make any reasonable assumption that will help you calculate the numbers
Vietnamese nationalists : Why do you think the Vietnamese nationalists begin by referring to the Declaration of Independence and the French Declaration of the Rights of Man and the Citizen?

Reviews

Write a Review

Programming Languages Questions & Answers

  Write down public static method using public interface

Using only public interface of UnorderedLinkedList class, write down a method: public static void reverse(UnorderedLinkedList staff) that reverses the entries in a linked list.

  Write program to ask user to enter number between a range

Write down program which asks user to enter number between 1 and 100. Use input validation to make sure that a valid number is entered before continuing program.

  Program to compute net pay of employee

A program to compute net pay of employee. It must permit one to read deposit number, account name, amount deposited, and yesr.check.

  Write method to accept stack containing some elements

Write a method that accepts a stack containing some elements from the file "Q1.txt" and returns a new stack containing the same elements and in the same order as the original stack.

  Design a class for string field for toppings

Design a class named pizza. Data fields include a string field for toppings (such as pepperoni) and numeric fields for diameter in inches (such as 12) and price (such as 13.99).

  Design program which ask the user for positive interger

Design a program which ask the user which application to do: after choosing from factorial or triangle(magicSquare), the program must then ask again the user for a positive interger.

  Designing the site links

Company needs to be able to schedule the time of day when the connections are available for replication. Replication should not occur during peak business hours. When designing the site links, which of the following options should you use?

  Define class that contains member variable of type string

Define a class named Document that contains a member variable of type String named text that stores any textual content for the document.

  Write unix shell script to check file in current directory

Write Unix shell script (one program) called Project1_lastname (your lastname). This script will do the following tasks: Using "if" statement checks for a file called "student.txt" in current directory.

  Differentiating heavyweight and lightweight process

What is the difference between a heavyweight and a lightweight process? Give an example of where heavyweight processes are appropriate.

  Program to processes the test data

Write a program that processes the test data. The output should be the student's ID, followed by the answers, followed by the test score, and followed by the test grade.

  Design a math sorting game

Using Visual Studio 2010, design a math sorting game. A random number between 1 and 99 will be generated and displayed to the user. User will quickly have to divide that number by 3, and come up with remainder.

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