Calculating the greatest common divisor

Assignment Help Programming Languages
Reference no: EM131860558

Assignment

Written Part

Write a math definition for Euclid's algorithm for calculating the greatest common divisor of two non-negative integers, and use equational reasoning (aka calculation, as shown in class and in the textbook (pp. 3-4, 5-6, 9, 11, 59-60, 61, 62, 63, 64, 65, 66, 75, 79, 83-84, . . . ) to show that

euclid 6 27 == 3

If you do not know/recall Euclid's algorithm, it is:
- if the two numbers are equal, then that number is the result
- otherwise the smaller number is subtracted from the larger, and we recurse with the result of the subtraction and the smaller number.


Programming Part

P2

Practice the five-step process given in the textbook, complete questions 7 and 8 from chapter 6. To wit,

§6.7Define a recursive function

merge :: Ord a ⇒ [a] → [a] → [a]

that merges two sorted lists to give a single sorted list. For example:

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

You do not need to show the five steps, but it might help you build the functions.

Note: your definition should not use other functions on sorted lists, such as insert or isort, but should be defined using explicit recursion.

§6.8Using your merge function, define a function

msort :: Ord a ⇒ [a] → [a]

that implements merge sort, in which an empty list and singleton lists are already sorted, and any other list is sorted by merging together two lists that result from sorting the two halves of the list separately.

Hint: first define a function

halve :: [a] → ([a], [a])

that splits a list into two halves whose lengths differ by at most one. Submit one file, abc123-a2q2.hs (where abc123 is replaced with your NSID) containing your solution and some test cases, including data other than just integers.

P3 Complete questions 7 and (an extended version of) 8 from chapter 7. To wit, extend the binary string-transmitter example on pp. 82-85 in the textbook,

import Data.Char type Bit = Int
bin2int :: [Bit] → Int
bin2int = foldr (λx y → x + 2×y) 0
unfold :: (a → Bool) → (a → b) → (a → a) → a → [b] unfold p h t x | p x = []
| otherwise = h x : unfold p h t (t x)
int2bin :: Int → [Bit]
int2bin = unfold (==0) (‘mod‘ 2) (‘div‘ 2)

make8 :: [Bit] → [Bit]
make8 bits = take 8 (bits ++ repeat 0)

encode :: String → [Bit]
encode = concat ? map (make8 ? int2bin ? ord)
chop8 :: [Bit] → [[Bit]]
chop8 = unfold null (take 8) (drop 8)

decode :: [Bit] → String
decode = map (chr ? bin2int) ? chop8
transmit :: String → String transmit = decode ? channel ? encode
channel :: [Bit] → [Bit] channel = id

test :: String → Bool test s = (s == transmit s)

s1 = "higher-order.functions.are.easy"
s2 = "learn.you.a.Haskell.for.great.good"

-- enter these at GHCi to run tests
-- test s1
-- test s2

by answering the following questions:

§7.7 Modify the binary-string transmitter example to detect simple transmis- sion errors using the concept of parity bits. That is, each eight-bit binary number produced during encoding is extended with a parity bit at the end, set to one if the number contains an odd number of ones, and to zero otherwise. In turn, each resulting nine-bit binary number consumed during decoding is checked to ensure that its parity bit is correct, with the parity bit being discarded if this is the case, and a parity error being reported otherwise.
Hint: the library function

error :: String → a

displays the given String as an error message and terminates the program; the polymorphic result type ensures error can be used in any context.

§7.8 Test your new string transmitter program from the previous exercise using three faulty communication channels:

a) a faulty communication channel that zeroes the second bit of every nine-bit binary number

b) a faulty communication channel that swaps the first and second bits of every nine-bit binary number

c) a faulty communication channel that forgets the first bit of every nine- bit binary number, which can be modelled using the tail function on lists of Bits

Submit three files,
abc123-a2q3a.hs, abc123-a2q3b.hs, and abc123-a2q3c.hs

(where abc123 is replaced with your NSID) containing your solutions to the corresponding parts along with test cases. Note that the first two faulty com- munications channels are occasionally correct-include examples that can be transmitted correctly and examples that cannnot be transmitted correctly.

Reference no: EM131860558

Questions Cloud

How to create a mission statement for a small business : Explain how to name a business, how to choose a business type, and how to create a mission statement for a small business.
Review the essays title as well as its introduction : Review the essay's title as well as its introduction and conclusion. Think about the relationships among these three components.
Reflect on the the state of global health : Reflect on the The State of Global Health Video (File attached below).What is the difference between developed and developing countries?
Examining strategic effectiveness of the new system : Vitality Health Enterprises, a medium-sized firm that manufactures health and personal care products, has experienced six straight quarters of strong revenue.
Calculating the greatest common divisor : CMPT 340-T1 - Write a math definition for Euclid's algorithm for calculating the greatest common divisor of two non-negative integers, and use equational reason
Dollar transaction exposure in dollar terms : What is Rolls-Royce’s dollar transaction exposure in dollar terms? In pound terms?
According to its director of export marketing : According to its director of export marketing, this simple strategy eliminates all its currency risk. Is he right? Why?
Discuss the possible consequences of the omission : Discuss and give an example when it might be appropriate to omit explanations from your instructions. Then, discuss the possible consequence(s) of the omission.
Assuming there is no forward market in ringgit : Assuming there is no forward market in ringgit, can you create a homemade forward contract that would allow your client to hedge its ringgit exposure?

Reviews

len1860558

2/12/2018 3:39:16 AM

Assignment 2 28, Due Date 11, at 11:55pm File Format one pdf file for the written part five Haskell files for the programming part Location the moodle dropbox : updated with fancy Haskell code formatting : initial -T2 release Submit three files, abc123-a2q3a.hs, abc123-a2q3b.hs, and abc123-a2q3c.hs (where abc123 is replaced with your NSID) containing your solutions to the corresponding parts along with test cases. Note that the first two faulty com- munications channels are occasionally correct—include examples that can be transmitted correctly and examples that cannnot be transmitted correctly.

len1860558

2/12/2018 3:38:46 AM

I only need question 1, P2 and P3 Submission Instructions Submit your answers as six files: 1.one PDF file abc123-a2.pdf containing your written answer to the written question. 2.five Haskell source files as described in the programming questions. Note that abc123 must be replaced with your NSID and that each file must contain an identity block giving • your name • your NSID • your student number • this course and assignment number

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