Ask Programming Language Expert

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.

Programming Language, Programming

  • Category:- Programming Language
  • Reference No.:- M92688912
  • Price:- $120

Guranteed 48 Hours Delivery, In Price:- $120

Have any Question?


Related Questions in Programming Language

Assignment - haskell program for regular expression

Assignment - Haskell Program for Regular Expression Matching Your assignment is to modify the slowgrep.hs Haskell program presented in class and the online notes, according to the instructions below. You may carry out th ...

Assignment task -q1 a the fibonacci numbers are the numbers

Assignment Task - Q1. (a) The Fibonacci numbers are the numbers in the following integer sequence, called the Fibonacci sequence, and are characterised by the fact that every number after the first two is the sum of the ...

Question - create a microsoft word macro using vba visual

Question - Create a Microsoft Word macro using VBA (Visual Basic for Applications). Name the macro "highlight." The macro should highlight every third line of text in a document. (Imagine creating highlighting that will ...

Assignmentquestion onegiving the following code snippet

Assignment Question One Giving the following code snippet. What kind of errors you will get and how can you correct it. A. public class HelloJava { public static void main(String args[]) { int x=10; int y=2; System.out.p ...

Assignment - proposal literature review research method1

Assignment - Proposal, Literature Review, Research Method 1. Abstract - Summary of the knowledge gap: problems of the existing research - Aim of the research, summary of what this project is to achieve - Summary of the a ...

1 write a function named check that has three parameters

1. Write a function named check () that has three parameters. The first parameter should accept an integer number, andthe second and third parameters should accept a double-precision number. The function body should just ...

Assignment - horse race meetingthe assignment will assess

Assignment - Horse Race Meeting The Assignment will assess competencies for ICTPRG524 Develop high level object-oriented class specifications. Summary The assignment is to design the classes that are necessary for the ad ...

Task silly name testeroverviewcontrol flow allows us to

Task: Silly Name Tester Overview Control flow allows us to alter the order in which our programs execute. Building on our knowledge of variables, we can now use control flow to create programs that perform more than just ...

Structs and enumsoverviewin this task you will create a

Structs and Enums Overview In this task you will create a knight database to help Camelot keep track of all of their knights. Instructions Lets get started. 1. What the topic 5 videos, these will guide you through buildi ...

Task working with arraysoverviewin this task you will

Task: Working with Arrays Overview In this task you will create a simple program which will create and work with an array of strings. This array will then be populated with values, printed out to the console, and then, w ...

  • 4,153,160 Questions Asked
  • 13,132 Experts
  • 2,558,936 Questions Answered

Ask Experts for help!!

Looking for Assignment Help?

Start excelling in your Courses, Get help with Assignment

Write us your full requirement for evaluation and you will receive response within 20 minutes turnaround time.

Ask Now Help with Problems, Get a Best Answer

Why might a bank avoid the use of interest rate swaps even

Why might a bank avoid the use of interest rate swaps, even when the institution is exposed to significant interest rate

Describe the difference between zero coupon bonds and

Describe the difference between zero coupon bonds and coupon bonds. Under what conditions will a coupon bond sell at a p

Compute the present value of an annuity of 880 per year

Compute the present value of an annuity of $ 880 per year for 16 years, given a discount rate of 6 percent per annum. As

Compute the present value of an 1150 payment made in ten

Compute the present value of an $1,150 payment made in ten years when the discount rate is 12 percent. (Do not round int

Compute the present value of an annuity of 699 per year

Compute the present value of an annuity of $ 699 per year for 19 years, given a discount rate of 6 percent per annum. As