act02ans

.pdf

School

Syracuse University *

*We aren’t endorsed by this school

Course

623

Subject

Computer Science

Date

Oct 30, 2023

Type

pdf

Pages

2

Uploaded by badMonk123

Report
Fall CIS Assured Programming with Formal Methods Act. 02 solution Name: SU email: Class student number: Introduction In this activity, you will develop two Haskell functions. By completing this activity successfully, you will be able to define simple Haskell functions via the basic constructs (as shown in Hutton, chapter ). In addition, you will be able to give an alternate definition of the function reverse in Haskell via recursion. Practice a. The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds as follows: consider each digit as a separate number; moving left, double every other number from the second last; subtract from each number that is now greater than ; add all the resulting numbers together; if the total is divisible by , the card number is valid. Define a function uhnDoub e :: Int -> Int that doubles a digit and subtracts 9 if the result is greater than . For example: > uhnDoub e 3 6 > uhnDoub e 6 3 Using uhnDoub e and the integer remainder function mod , define a function uhn :: Int -> Int -> Int -> Int -> Boo that decides if a four-digit bank card number is valid. For example: > uhn 1 7 8 4 True > uhn 4 7 8 3 Fa se Andrew C. Lee EECS Dept, Syracuse University
Fall CIS Assured Programming with Formal Methods Act. 02 solution Answer > uhnDoub e :: Int -> Int > uhnDoub e d = if n > 9 then n-9 e se n > where n = d*2 > uhn :: Int -> Int -> Int -> Int -> Boo > uhn a b c d = ((x + b + y + d) mod 10) == 0 > where > x = uhnDoub e a > y = uhnDoub e c b. Give an alternate implementation of the function reverse that does not require the use of the functions in prelude. Instead, write helper functions to complete the implementation. Answer Original definition of reverse (written as reverse1 below): > reverse1 :: [a] -> [a] > reverse1 [] = [] > reverse1 (x:xs) = reverse1 xs ++ [x] Another definition based on the idea of using stacks. > transfer :: ([a],[a]) -> ([a],[a]) > transfer ([] , ys) = ([], ys) > transfer ((x:xs) , ys) = transfer (xs, (x:ys)) > second :: (a,b) -> b > second (x,y) = y > reverse :: [a] -> [a] > reverse xs = (second.transfer) (xs, []) Andrew C. Lee EECS Dept, Syracuse University
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help

Browse Popular Homework Q&A

Q: a) A crate weighing 6000 N is suspended from two cables, as shown. Determine the magnitude of the…
Q: Question # 1: Make a Routh table to determine how many roots of the following polynomial are in the…
Q: For each eigenvalue of A, find an associ Find a diagonal matrix D and an inver 0 1 0 1.1 0 0 01 0. 0…
Q: (a) For the system of space probes and launcher, is the force of the exploding bolts: O Internal O…
Q: 1. Let v, w be any two vectors. Prove that ||v + w||² - ||v − w||² = 4v. w W (Hint: Use properties…
Q: 2. Method of Joints: Using the method of joints (no other method is acceptable), determine the…
Q: Identify the differences between the three education pathways to RN education. What are the…
Q: What specific qualities of ATP make it a useful energy currency?
Q: What is the resistance of a 4.00x10³, a 2.00x10³, and a 3.00x10³ resistor connected in series?…
Q: If the value of your house is $263,000 today and inflation is constant at 10.9% annually, what will…
Q: A company’s raw data sample of weekly salaries (in dollars) is shown below. 210   370   530   370…
Q: A lamp of mass m is hung from from a series of short massless wire segments to a horizontal ceiling…
Q: 15 Give the organic product: A. C. á 03 (8) Zn/H₂O B. D. ? ОН OH OH он
Q: see attachment
Q: What does the term “free flowing” mean in organic chemistry lab?
Q: Q5: The accompanying figure shows a frictionless channel in the shape of a segment of a circle with…
Q: 8 ft Determine the volume of the shaded region. T 7 ft 15 ft 7 ft 4 ft The volume of the shaded…
Q: Show your calculations for the following questions, the initial amount of compound is 5 g and its…
Q: Find the height of a column of water where the pressure at the bottom is 115 k
Q: J. J. Thomson is best known for his discoveries about the nature of cathode rays. His other…
Q: What were the Middle Class values that emerged in the Gilded Age?
Q: are the IT manager in charge of a large library software system which fails unexpectedly one Monday…