List comprehension to return a list of all the numbers

Assignment Help Programming Languages
Reference no: EM13330999

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 fevaluates 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 listTo. This function should return a list of all the prime numbers up to a given values.

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: EM13330999

Questions Cloud

State balance including physical states and solubility rules : Complete and Balance including physical states and solubility rules. MgCL2 (aq) + K3PO4 (aq)
Calculate the angular speed of the stick : A meter stick with a mass of 0.159kg is pivoted about one end so it can rotate without friction about a horizontal axis. Calculate the angular speed of the stick
County on several counts of selling an obscene book : In a grand jury case, a bookstore was indicted in Oklahoma County on several counts of selling an obscene book
Problem caused by each control weakness : Identify the missing internal control characteristic in each situation and identify the possible problem caused by each control weakness.
List comprehension to return a list of all the numbers : 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 fevaluates to True.
The states of oregon and washington : Douglas Boats is a supplier of boating equipment for the states of Oregon and Washington.
Advantages and disadvantages of forming the business : Provide a summary to the partners, outlining the advantages and disadvantages of forming the business as a partnership and the advantages and disadvantages of forming as a corporation
Explain water are present in one mole of anhydrous salts : A solid hydrate weighing 2.691g was heated to drive off the water. A solid anhydrous residue remained, which weighed 2.259g. If the anhydrous residue has a formula weight of 282, How much moles of water are present in one mole of anhydrous salts
Balance sheet of the contractor : A building contractor took a contract for the construction of a certain building on 1st January 1994. The contract price was agreed at Rs 4,00,000. The contractor had made the following expenditure during the year.

Reviews

Write a Review

Programming Languages Questions & Answers

  Write a haskell program to calculates a balanced partition

Write a program in Haskell which calculates a balanced partition of N items where each item has a value between 0 and K such that the difference b/w the sum of the values of first partition,

  Create an application to run in the amazon ec2 service

In this project you will create an application to run in the Amazon EC2 service and you will also create a client that can run on local machine and access your application.

  Explain the process to develop a web page locally

Explain the process to develop a Web page locally

  Write functions

These 14 questions covers java class, Array, link list , generic class.

  Programming assignment

If the user wants to read the input from a file, then the output will also go into a different file . If the user wants to read the input interactively, then the output will go to the screen .

  Write a prolog program using swi proglog

Write a Prolog program using swi proglog

  Create a custom application using eclipse

Create a custom Application Using Eclipse Android Development

  Create a application using the mvc architecture

create a application using the MVC architecture. No scripting elements are allowed in JSP pages.

  Develops bespoke solutions for the rubber industry

Develops bespoke solutions for the rubber industry

  Design a program that models the worms behavior

Design a program that models the worm's behavior.

  Writing a class

Build a class for a type called Fraction

  Design a program that assigns seats on an airplane

Write a program that allows an instructor to keep a grade book and also design and implement a program that assigns seats on an airplane.

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