CS 119 Lab 4 – Currying
Objectives
Perform the following tasks in the order given.
A self-verifying number with rightmost
digit d1, second digit d2, etc. will satisfy the
property
f(1,d1) + f(2,d2) + f(3,d3) + ... is divisible by m
for some specific function f and divisor m. We will write a function makeVerifier that takes as arguments f and m and returns a function that will perform a verification test for a given number. In other words makeVerifier is a function factory that makes the verifying functions for us. Before we do this, let's take a look one particular function that this factory is supposed to produce. Suppose we want to check that the sum of the digits is divisible by 17. That is, f(i,di) = di and m is 17.
The lab4 code gives you functions to get the last
digit of a number (by using the remainder of division by 10) and everything
but the last digit (by using integer division). We can use these to
create a function sumOfDigits (also provided). Make sure that
you understand this code. Try it out.
| Assignment: Write a function divisible :: Integer -> Integer -> Bool which takes a divisor m and a number num and determines whether the sum of the digits of num is divisible by m |
We will now generalize this approach to achieve our function factory makeVerifier.
|
Assignment: Write a function digitSum which takes a function f and a number num and computes f(1,d1) + f(2,d2) + f(3,d3) + ... for all the digits in num. |
|
Assignment: Write the function makeVerifier :: (Integer->Integer->Integer) -> Integer -> (Integer -> Bool) makeVerifier f m = ... |
checkISBN = makeVerifier (*) 11
Try checking the numbers on some of your textbooks. Also try making some simple errors and see if the numbers verify.
|
Assignment: UPC barcodes use a divisor of 10 and the function f(i,di) = di when i is odd and 3di when i is even. Build a verifier checkUPC and test it out on some UPC codes. (The UPC number consists of all the digits: the one to the left of the bars, the ones underneath the bars, and the one on the right.) |
|
Assignment: Credit card numbers also have a divisor of 10 and use a function that yields di when i is odd. When i is even, the function returns 2di if di < 5 and 2di + 1 otherwise. Build a verifier for checking credit card numbers and test it out. |