CS 291 Assignment #5

Due Thursday, March 4th (by 11:59pm)
Not accepted after March 8th at class time

Introduction:

The assignment this week will have you write some Java code to contrast with Haskell we've already written, then get some practice computing with infinite lists and working through the mechanics of lazy evaluation.

The Assignment

  1. In class we went over the book's solution for finding the edit distance between a pair of strings. (The code we wrote in class is here.) As an exercise, write a solution to this problem in Java. You can write a recursive solution modeled after our Haskell version, an iterative version, or anything else you wish. (Make sure it's your own work and not something you found via Google though!) For full credit your Java version should have a type to describe the edit operations like the Haskell version does, your transform method should be static, and you should implement toString methods as necessary so that it's possible to display your edit sequences as shown below (copied and pasted from the DrJava interactions pane). Note: An enumerated type is almost enough to represent the edits, but can't capture the Change and Insert operations since they need to "remember" the character associated with the operation.
    > EditDistance.transform("brad", "")
    [Kill]
    > EditDistance.transform("recursion", "cursin")
    [Delete, Delete, Copy, Copy, Copy, Copy, Copy, Delete, Copy]
    > EditDistance.transform("brad", "richards")
    [Delete, Copy, Insert i, Insert c, Insert h, Copy, Insert r, Copy, Insert s]
    

    Once you're done, tell me two things you like better about the Haskell version, and two things you like better about the Java version.

  2. Define a function pairSatisfies with type (a -> a -> Bool) -> [a] -> a. It takes a predicate of two arguments and an infinite list as inputs, and applies the predicate to neighboring items in the list until the predicate is satisfied, at which point it returns the first of the two items from the stream.
  3. Main> pairSatisfies (==) ["ab", "ba", "ba"]
    "ba"
    Main> pairSatisfies (>) [1,2,3,2,1]
    3
    Main> pairSatisfies (\n m -> (n*m) > 0) [-1,2,-3,-4]
    -3
    

  4. Define a function sqrtApprox that generates an infinite stream of approximations to the square root of a number. It takes two numbers as inputs: the initial guess at the square root, and the number whose root is sought. To improve any given guess at the square root of a number N, use the formula
    new guess = (guess + N/guess) / 2
    Sample runs:
    Main> take 5 (sqrtApprox 1.5 10)
    [1.5,4.08333,3.26616,3.16393,3.16228]
    Main> take 10 (sqrtApprox 1.5 25346)
    [1.5,8449.42,4226.21,2116.1,1064.04,543.93,295.264,190.553,161.783,159.225]
    
  5. Define a function sqroot that takes a single number as input and returns its square root. Use the function above to generate a stream of approximations (use 1.5 as the starting guess for any input), and stop when successive approximations are within 0.0001 of each other. (Use pairSatisfies to help detect this termination condition.)

  6. Given the Haskell code shown below, show the lazy evaluation steps required to produce the first two elements in the stream resulting from the evaluation of the expression increasing. When you stop, your solution should look like v1:v2:expr, where v1 is the first element in the stream (list), v2 is the second, and expr is the expression that remains to be evaluated. Your example should be typed so I have some hope of reading it, and you should underline the expression being evaluated at each step as was done on the trace from class. (You need not provide the explanations for each step though.)
    join f (n:ns) (m:ms) = (f n m) : (join f ns ms)
    
    ones = 1 : ones
    
    increasing = join (+) ones (0:increasing)
    

Submitting:

Write your lazy evaluation trace as a block comment in your .hs file, and submit your Java and Haskell code as separate attachments to an E-mail to me at brichards@pugetsound.edu. Please do not just copy and paste your solutions into the body of the E-mail! (If your E-mail client insists on including the content of text files into the body of the message, please zip the files up and attach the archive.)


Brad Richards, 2010