CS 119 Lab 4Currying

Objectives

  • Use currying to create a function that produces another function.
  • Perform the following tasks in the order given.

    1. Download the Lab4 project and import it into Eclipse as a Haskell project.
       
    2. Have you ever wondered how a sales representative knows when they have typed in an incorrect credit card number or a scanner knows when it fails to read a UPC barcode correctly?  These are examples of self-verifying numbers.  They are designed to have a certain property that will fail if a simple error occurs like changing a digit.

      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.
       


    3.  
      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


       

    4. If we define

          divisibleBy17 = divisible 17

      We have used currying to get a new function.  The expression divisibleBy17 num will now verify that the sum of the digits of num is divisible by 17.

      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.


       

    5. Now putting it together.
       
      Assignment:
      Write the function makeVerifier :: (Integer->Integer->Integer) -> Integer -> (Integer -> Bool)
      makeVerifier f m = ...


       

    6. A simple example of a self-verifying number is an ISBN number.  The ISBN numbers uses the function f(i,di) = i*di and a divisor of 11.  So we could define:

      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.)


       


    7.  
      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.


       

    8. Email your modified zipped project to me for grading.